В Firebase onMessageReceived(RemoteMessage remoteMessage)
этот метод будет вызываться, когда ваше приложение находится в Foreground. Итак, при нажатии на уведомление вы можете открыть другое действие, скажем NotiicationActivity
.Firebase onMessageReceived (RemoteMessage remoteMessage), не вызывается, когда приложение находится в фоновом режиме
Но что, если ваше приложение находится в фоновом режиме, этот метод не будет вызываться, и при нажатии на уведомление будет открыта только деятельность Launcher.
Так, как открыть NotificationActivity
при нажатии уведомления, даже если наше приложение находится в фоновом режиме.
Мой код заключается в следующем:
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "MyFirebaseMsgService";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d(TAG, "From: " + remoteMessage.getFrom());
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
}
if (remoteMessage.getNotification() != null) {
Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
}
sendNotification(remoteMessage.getNotification().getBody());
}
private void sendNotification(String messageBody) {
Intent intent = new Intent(this, NewActivity.class);
intent.putExtra("key", messageBody);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
Bitmap icon2 = BitmapFactory.decodeResource(getResources(),
R.mipmap.ic_launcher);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("FCM Sample")
.setContentText(messageBody)
.setAutoCancel(true)
.setLargeIcon(icon2)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(new Random().nextInt() /* ID of notification */, notificationBuilder.build());
}
}
Прочитайте этот ответ: sloved вопрос [здесь ссылка] (http://stackoverflow.com/questions/ 40626233/firebase-onmessagereceived-not-called-when-app-isactive/40626866 # 40626866) –