8

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?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
redochka
  • 12,345
  • 14
  • 66
  • 79
  • 1
    Can you post your payload? – AL. Jun 03 '16 at 02:20
  • What do you mean using the new FCM? Are you sending the messages from your app server, or are you using the Firebase composer to send the messages? – Arthur Thompson Jun 04 '16 at 02:21
  • yes i am sending from my server – redochka Jun 04 '16 at 10:39
  • in my case onMessageReceived() is triggered when app is in foreground and notification received in tray when app is in background but when app process is killed from overview stack no notification was received!! Bug?? – Sjd Oct 21 '16 at 12:17
  • 2
    Same here i Checked that onMessageReceived() is triggered at all time when app is in background,foreground and killed and notification is also arrived in tray when app is in background and foreground but not when it is destroyed(killed) . I don't know what happen when notification arrived in app killed mode,but not shown on notification tray. – Hiren Dec 16 '16 at 10:20

4 Answers4

7

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

taman neupane
  • 938
  • 9
  • 18
  • I reckon I've noticed something similar... If I launch from the debugger and immediately kill, I don't see notifications in the tray. If I launch again and then kill, I do! – Ian Warburton May 03 '18 at 13:44
  • Yes you can build your application in release variant so that you can get notification after swipe close too. – taman neupane May 07 '18 at 06:11
  • Thank you so much, it worked for me, if I launch my app from Android Studio and then kill it, the notification won't appear but if I launch my app from the launcher icon and kill it afterwards, the notification will appear, that's bollocks – Armando Ávila Bueno Oct 18 '20 at 20:36
3

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"
    }
  }
AL.
  • 36,815
  • 10
  • 142
  • 281
Avijit
  • 1,253
  • 13
  • 21
  • 1
    You are right! Adding the notification object show the notification if the notification tray even when app is closed. However, i wanted my app to get the payload without having the user to click on the notification. Is there some way to achieve this? – redochka Jun 04 '16 at 16:01
  • No, it will show in notification tray only when app is closed and on tapping that notification you can show other information from the payload. – Avijit Jun 04 '16 at 18:49
  • I noticed that when the notification object is added to the push message, the payload is not delivered to the app even when it is running in the background and which requires the user to tap the notification before the payload get delivered. This is a no-go. This is different from GCM. – redochka Jun 05 '16 at 12:01
  • 1
    I still don't receive the notification when app is killed, using same json. – WISHY Mar 10 '17 at 06:59
1

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.

Pado
  • 1,497
  • 16
  • 28
Usher
  • 369
  • 4
  • 14
  • I am not looking to get the notification in the tray bar. I want the payload to be delivered to the app in the background. – redochka Jan 10 '17 at 21:49
  • I still don't receive the notification when app is killed, using same json. – WISHY Mar 10 '17 at 06:59
0

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());
    }
Bills
  • 768
  • 7
  • 19