Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 329
redochka Avatar asked Jun 02 '16 23:06

redochka


3 Answers

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

like image 132
taman neupane Avatar answered Oct 24 '22 08:10

taman neupane


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"
    }
  }
like image 25
Avijit Avatar answered Oct 24 '22 08:10

Avijit


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.

like image 1
Usher Avatar answered Oct 24 '22 09:10

Usher