Push notification received while the app is in the foreground is different from push notification received when the app is in background

与世无争的帅哥 提交于 2019-12-11 05:57:11

问题


I have implemented FCM push notifications in an android app.

While I am logged in to the app. I get the notification in the form that I am expecting as below.

When the app is in background I receive the json response instead as below.

Following is the code I have added in onMessageRecieved()

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setContentTitle("App")
            .setContentText(messageBody)
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentIntent(pendingIntent)
            .setStyle(new NotificationCompat.BigPictureStyle()
                    .bigPicture(bitmap))/*Notification with Image*/;

How can I get the notification in the same manner in both cases.

Any help will be appreciated thank you


回答1:


After setting up FCM in Android application, you can use Firebase console to send notifications. When foregrounded application receives a notification, onMessageReceived method is invoked. You should override this method to handle notification, but the problem is when the application is in the background and receives a notification, notification delivers to the device’s system tray and you can not handle notification with onMessageReceived method. When the user taps on a notification, it opens the app launcher by default. For example consider you want to do a specific task when a notification is received to the user or do something in background without the user realizing or don’t want to show notification dialog to the user, you can’t do these when the application is backgrounded.

Message Types

With FCM you can send two types of messages to clients :

1- Notification message, sometimes thought of a “display message”

2- Data message, which are handled by the client application

According to Google documents a notification message has a 2KB limit and a predefined user-visible keys. Data messages let developers send up to 4KB custom key-value pairs.

Solution:

If you want to handle notification when the application is backgrounded you should send data message and use onMessageReceived method.

Here is the complete article.




回答2:


That is so because push notifications handles in foreground and background in different ways.

From the documentation

Notification messages delivered when your app is in the background. In this case, the notification is delivered to the device’s system tray. A user tap on a notification opens the app launcher by default.

Messages with both notification and data payload, both background and foreground. In this case, the notification is delivered to the device’s system tray, and the data payload is delivered in the extras of the intent of your launcher Activity.

Also you have to remember that there are 2 types of firebase pushes - Notification message and Data message.

Looks like you are using Notification messages, so they are working as expected.

Just use data type of messages and so you can always handle them as you need.



来源:https://stackoverflow.com/questions/50854511/push-notification-received-while-the-app-is-in-the-foreground-is-different-from

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!