redochka
redochka

Reputation: 12819

Push not received when app is killed

I am using the new FCM to push message from my server to my android app.

{
    "to" : "APA91bHun4MxP5egoKMwt2KZFBaFUH-1RYqx...",
    "data" : {
      "Nick" : "Mario",
      "Room" : "PortugalVSDenmark"
    }
}

I am able to push messages to my app using the new FCM however, when I kill the app (long press on home button then slide the app to the left), push messages are not delivered any more.

Why?

Upvotes: 8

Views: 9567

Answers (4)

Bills
Bills

Reputation: 798

Your notification JSON should include

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

to show message in notification bar using firebase's default functionality when application is closed. Check out this for more info

Alternately

onMessageReceived should trigger in all cases, so you can generate notification using

 private void sendNotification(String messageBody) {
        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
                PendingIntent.FLAG_ONE_SHOT);

        Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.ic_launcher)
                .setContentTitle("FCM Message")
                .setContentText(messageBody)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);

        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
    }

Upvotes: 0

Usher
Usher

Reputation: 379

The key to this issue is that you have to create exact same json format :

{
    "to" : "APA91bHun4MxP5egoKMwt2KZFBaFUH-1RYqx...",
    "notification" : {
      "body" : "You have a new message",
      "title" : "",
      "icon" : "myicon" // Here you can put your app icon name
    }
}

I use to miss the "icon" tag, which makes me can only get notification when the app is in background. Once you have the correct json format, you will be able to get the notification when the app is killed.

Upvotes: 1

taman neupane
taman neupane

Reputation: 938

I've noticed that I get different behaviour depending on how the app was launched before I swiped it closed, as bizarre as that sounds.

If I launched the app with a debugger, and swipe it off, then I no longer receive FCM notifications. If I then launch the app from the launcher icon, I receive FCM notifications, and if I swipe off that 'launched from the launcher' app, I continue to receive FCM notifications. I get this behaviour on a Galaxy S4 and a Sony Xperia Z2.

Given that the easiest way for me to get the latest version of the app on the phone onto the app is to hit debug, this is something that I have to keep remembering:

When you swipe it closed, launch it from the launcher icon again, and swipe it closed again. (At least if you're testing FCM notification behaviour when the app isn't running).

Taken from : https://github.com/firebase/quickstart-android/issues/41

Upvotes: 7

Avijit
Avijit

Reputation: 1313

Check the raw payload, what is received from server. body and title key have to have under notification key to get push-notification when app is closed. Though you'll only get notifications in notification tray when app is closed. Sample payload :

{
    "to" : "APA91bHun4MxP5egoKMwt2KZFBaFUH-1RYqx...",
    "notification" : {
      "body" : "great match!",
      "title" : "Portugal vs. Denmark",
      "icon" : "myicon"
    },
    "data" : {
      "Nick" : "Mario",
      "Room" : "PortugalVSDenmark"
    }
  }

Upvotes: 3

Related Questions