Search code examples
androidfirebaseandroid-activitypush-notificationfirebase-notifications

MyFirebaseMessagingService not called when activity is killed


When the application is alive everything is running properly but when no activity is running I still get a notification with app_name and Body by firebase notification and opens MainActivity.
here is my MyFirebaseMessagingService class:-
I saw a similar question but it did not help me in my case.

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    @Override
public void onMessageReceived(RemoteMessage remoteMessage) {

    if(remoteMessage.getNotification()!=null){
        if(DictionarySscWords.send_notifications) { //tried removing this condition but same result.
            sendnoti(remoteMessage.getNotification().getBody());
        }
    }
}

private void sendnoti(String body) {
   Intent i1=new Intent(this,MainActivity.class);
    i1.putExtra("word", body);
    i1.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pi=PendingIntent.getActivity(this,0,i1,PendingIntent.FLAG_CANCEL_CURRENT);
    Uri ns=RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder nb=new NotificationCompat.Builder(this).setSmallIcon(R.drawable.heart)
            .setContentTitle("Word Of the Day")
            .setContentText(body).setSmallIcon(R.drawable.heart)
            .setAutoCancel(true)
            .setSound(ns)
            .setContentIntent(pi);
    NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(0,nb.build());
}

}

when Application is alive

enter image description here

when no activity is running

enter image description here Also Tried to run through AVD but No error.
Am confused how notification is created without passing through this class as if this was called different value will be populated here.
thanks


Solution

  • As it says on Firebase Documentation:

    Notification messages are delivered to the notification tray when the app is in the background. For foreground apps, messages are processed by these callbacks:

    DidReceiveRemoteNotification: on iOS OnMessageReceived () on Android. The notification key in the data packet contains the notification.

    If you are using a message based notification, then you will handle then only when your application is in the foreground, otherwise, Firebase will handle them.

    Example of notification based on message:

    {
        "to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
        "notification" : {
          "body" : "great match!",
          "title" : "Portugal vs. Denmark",
          "icon" : "myicon"
        }
      }
    

    If you wanna to handle all your notification, independent if the application is in foreground or background, you will need to start sending push notification based on data. As example:

    {
       "to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
       "data" : {
         "Nick" : "Mario",
         "body" : "great match!",
         "Room" : "PortugalVSDenmark"
       },
     }