我有一個serius問題。 在我的Android應用我有這樣的代碼:在Android中點擊通知時的Android getExtras()
類:MessagingService.class
public void onMessageReceived(RemoteMessage remoteMessage) {
String title = remoteMessage.getNotification().getTitle();
String messaggio =remoteMessage.getNotification().getBody();
String data = remoteMessage.getData().get("json");
JSON json = new JSON();
String fragment = json.getNotificaFragment(data);
Log.d("MessagingService","Fragment Ricevuto: " + fragment);
int requestID = (int) System.currentTimeMillis();
Intent intent = new Intent(this, LoginActivity.class);
intent.putExtra("fragment", fragment); //Put your id to your next Intent
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, requestID, intent, PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
notificationBuilder.setContentTitle(title);
notificationBuilder.setContentText(messaggio);
notificationBuilder.setSmallIcon(R.mipmap.ic_launcher);
notificationBuilder.setVibrate(new long[] { 500, 500 });
notificationBuilder.setSound(Settings.System.DEFAULT_NOTIFICATION_URI);
notificationBuilder.setAutoCancel(true);
notificationBuilder.setContentIntent(pendingIntent);
notificationBuilder.setDefaults(Notification.DEFAULT_VIBRATE | Notification.DEFAULT_SOUND | Notification.FLAG_SHOW_LIGHTS);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
super.onMessageReceived(remoteMessage);
}
類:LoginActivity.class
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
Log.d("APP", "AVVIO APPLICAZIONE");
String fragmentDaAprire = getIntent().getStringExtra("fragment");
Log.e("Bundle","Fragment da Aprire: " + fragmentDaAprire);
的問題是:
如果我收到通知,而我的應用程序是開放的結果礦石正確:
01-15 15:09:12.301 11419-11419/livio.***E/Bundle: Fragment da Aprire: messaggi
如果我收到通知,而我的應用程序是CLOSE,當我的通知點擊導致不正確的:(
01-15 15:09:12.301 11419-11419/livio.*** E/Bundle: Fragment da Aprire: null
感謝您的幫助
使用getIntent()getExtras() – Nas
我很抱歉,但沒有作品。 –