Push not received when application is killed

I am using the new FCM to output a message from my server to my Android application.

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

I can send messages to my application using the new FCM, however, when I kill the application (long press the home button and then move the application to the left), push messages are no longer delivered.

Why?

+5
source share
4 answers

Check the raw payload received from the server. The body and title must have a notification button below to receive a push notification when the application is closed. Although you will receive notifications in the notification tray when the application is closed. Payload Example:

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

I noticed that I get different behavior depending on how the application was launched before I closed it, as strange as it sounds.

If I run the application using the debugger and uninstall it, I no longer receive FCM notifications. If I launch the application from the launch icon, I receive FCM notifications, and if I remove this "launch from launch application" application, I continue to receive FCM notifications. I get this behavior on the Galaxy S4 and Sony Xperia Z2.

Given that the easiest way to get the latest version of the application on the phone in the application is to debug, this is what I should remember:

When you touch it, start it again from the start icon and close it again. (At least if you are testing FCM notification behavior when the application is not running).

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

+1
source

The key to this problem is that you need to create the 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 the skip tag "icon", which forces me to receive a notification only when the application is in the background. Once you get the correct json format, you will be able to receive a notification when the application is killed.

0
source

Your JSON notification should include

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

to show a message in the notification panel using the default functionality for firebase when the application is closed. this for more information

On the other hand

onMessageReceived should work in all cases, so you can generate a 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()); } 
0
source

All Articles