Когда я создаю уведомление нажатием кнопки в приложении, оно правильно открывает NotificationActivity. Но когда уведомление генерируется FCM (через MyFirebaseMessagingService), нажатие на уведомление вместо этого открывает MainActivity.
Вот соответствующие части моего кода и конфигурации:
1. Служба обмена сообщениями Firebase
Код: Выделить всё
public class MyFirebaseMessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
if (remoteMessage.getNotification() != null) {
showNotification(
getApplicationContext(),
remoteMessage.getNotification().getTitle(),
remoteMessage.getNotification().getBody()
);
}
}
public static void showNotification(Context context, String title, String message) {
String channelId = "general";
Intent intent = new Intent(context, NotificationActivity.class);
intent.setAction("OPEN_NOTIFICATION_ACTIVITY");
intent.putExtra("notification_title", title);
intent.putExtra("notification_body", message);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(
context,
0,
intent,
PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT
);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context, channelId)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(title)
.setContentText(message)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setContentIntent(pendingIntent)
.setAutoCancel(true);
NotificationManager notificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(
channelId, "General", NotificationManager.IMPORTANCE_HIGH);
notificationManager.createNotificationChannel(channel);
}
notificationManager.notify(1, mBuilder.build());
}
}
Код: Выделить всё
Ожидаемое поведение: при нажатии на уведомление должна открыться NotificationActivity, отображающая заголовок и текст, переданные в намерении.Фактическое поведение: вместо NotificationActivity открывается MainActivity.
Подробнее здесь: https://stackoverflow.com/questions/792 ... et-notific
Мобильная версия