У меня есть приложение, которое отвечает на сообщения Firebase, публикует уведомление на устройстве, и когда пользователь нажимает на уведомление, он должен перезапустить приложение с небольшим количеством информации, которая пришла с сообщением Firebase (это одно<20 char String). </p>
// In response to onMessageReceived() in a FirebaseMessagingService object...
// 1. create the intent to bundle with the notification and add custom data
Context appContext = this.getApplicationContext();
Intent intent = new Intent(appContext, MainActivity.class);
intent.putExtra("myData",myData); // myData is a simple string passed from FCM - verified it's correct
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); // per docs/StackOverflow
intent.setAction(Intent.ACTION_VIEW);
// 2. package as a one shot pending intent that is fired when the user taps it in system notification list
int requestCode = (int) System.currentTimeMillis();
PendingIntent pendingIntent =
PendingIntent.getActivity(myActivity, requestCode, intent,
PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_UPDATE_CURRENT); // tried with/without FLAG_UPDATE_CURRENT
// 3. build a notification around launching the intent
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(appContext)
.setSmallIcon(R.drawable.mylogo)
.setContentTitle(messageTitle)
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setContentIntent(pendingIntent);
// 4. send notification to the system
NotificationManager notificationManager =
(NotificationManager) appContext.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify((int) System.currentTimeMillis(), notificationBuilder.build());
Получено сообщение Firebase, и уведомление успешно отправлено в службу системных уведомлений. Когда пользователь нажимает на уведомление, приложение переносится на передний план, но с тем же намерением, которое было первоначально передано приложению, т.е. без myData в комплекте с намерением. Я пытался получить намерение в onResume и onCreate, и результаты совпадают - ему не хватает данных, связанных с уведомлением.
// Back in the Main activity
Intent intent = this.getIntent();
String myData = intent.getStringExtra("myData"); // always null
doAction(myData); // fails because myData always null
Я прочесывал StackOverflow и сеть для ответов и до сих порЯ перепробовал множество вариантов, но результаты остались прежними - myData никогда не пересылается.