8

In Firebase onMessageReceived(RemoteMessage remoteMessage), this method will be called when your app is in Foreground. So, on clicking the notification you can open a different Activity, say NotiicationActivity.

But what if your app is in background, then this method will not be called, and on clicking the notification only a Launcher Activity will be opened.

So how to open the NotificationActivity on clicking the notification even if our app is in background.

My Code is this :

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());
    }
}
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Aman Shekhar
  • 2,719
  • 1
  • 18
  • 29
  • 1
    Read this answer : sloved the issue [here is the link](http://stackoverflow.com/questions/40626233/firebase-onmessagereceived-not-called-when-app-is-inactive/40626866#40626866) – Raut Darpan Dec 07 '16 at 05:40

3 Answers3

4

onMessageReceived is only triggered when the application is in foreground. If the app is backgrounded, you may still be able to receive the notification but onMessageReceived will not be triggered.

So my suggestion, on the receiving activity, you can still get the data from the notification by using:

getIntent().getExtras();

This should work accordingly. Hope this helps :)

kenix
  • 124
  • 6
  • Thanks this works, I just did this. `if (getIntent().getExtras() != null) { // I Wrote here. Intent intent = new Intent(MainActivity.this, NotificationActivity.class); startActivity(intent); }` – Aman Shekhar Dec 07 '16 at 06:56
  • No worries @Shekhar , please vote it up so other may know that this solution works. Thank you. – kenix Dec 08 '16 at 07:43
  • There are two types of messages data messages and notification messages. Data messages are handled here in onMessageReceived whether the app is in the foreground or background. Notifications messages only when the app is in background – Dan Alboteanu Oct 26 '18 at 15:02
0

try and follow below link :

Firebase Cloud Messaging for Android

Prakash Gajera
  • 563
  • 1
  • 6
  • 18
  • This tutorial is working only when your app is in foreground. I want to open the NotificationActivity even if the app is in background, or not even launched. – Aman Shekhar Dec 07 '16 at 05:35
0

In your sendNotification() method :

 private void showNotification(String message){
    Intent intent=new Intent(this, NotificationActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent.putExtra("key", messageBody);
    PendingIntent pendingIntent=PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder builder=new NotificationCompat.Builder(this)
            .setAutoCancel(true)
            .setContentTitle("FCM Sample")
            .setContentText(message)
            .setLargeIcon(icon2)
            .setContentIntent(pendingIntent);
    NotificationManager manager=(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    manager.notify(0,builder.build());

}
Shweta Chauhan
  • 6,739
  • 6
  • 37
  • 57
  • Please find the below link to get the correct solution for the problem. https://stackoverflow.com/a/56660363/1752341 – vishnuc156 Jan 03 '20 at 11:43