Handle data payload without clicking on notification?

When in the background, applications receive the notification payload in the notification tray and only process the data payload when the user clicks on the notification.

Is there a way to handle the data payload without clicking on the notification?

+3
source share
3 answers

Based on FCM docs message processing :

App state Notification Data Both Foreground onMessageReceived onMessageReceived onMessageReceived Background System tray onMessageReceived Notification: system tray Data: in extras of the intent. 

It is not specifically processed ONLY by clicking on the notification. You can handle it in onMessageReceived() . Pretty sure that the action of a click also depends on how you implement it.

If you intend to do something else, provide more details.

+1
source

If you want to receive a data payload when the application is not working , you need to use a custom application server with the http protocol (in your case) , which will send only the data payload without notification and send the data to the fcm endpoint as follows

  { "data": { "some_key" : "some_value", "some_more_key" : "some_more_value" }, "registration_ids": ["device-token","device-token2"] } 

or only to send data from the server to one client

 { "to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...", "data" : { "Nick" : "Test", "body" : "Your message here", "Room" : "Some Test Room" }, } 

Then, using POST , you can send a downstream message to your device, and it will work the way you want. If you want to have a basic understanding of such an application server, please think about this: How to send a notification to a client when Firebase has a new entry?

I have posted the basic Java servlet code here as well as the node.js code of the official firebase blog.

Hope this helps.

0
source

FCM message processing

To receive messages, use a service that extends FirebaseMessagingService. Your service should override the onMessageReceived callback that is provided for most types of messages, with the following exceptions:

Notifications sent when your app is in the background. In this case, the notification is delivered to the system tray of devices. The user, by clicking on the notification, opens the default application launcher.

Messages with a payload of notifications and data, both in the background and in the foreground. In this case, the notification is delivered to the deviceโ€™s system tray, and the data payload is delivered in addition to the purpose of your launch activity.

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

  • Notification messages, which are sometimes perceived as "display messages."
  • Data messages that are processed by the client application.

The notification message is a lighter option with a 2 KB limit and a predefined set of user-visible keys. Data messages allow developers to send up to 4 KB of custom key-value pairs. Notification messages may contain an additional data payload that is delivered when users click on the notification.

To process notifications in the background, you need to send data in data messages

Example: -

 { "to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...", "data" : { "Nick" : "Mario", "body" : "great match!", "Room" : "PortugalVSDenmark" }, } 

Please check these links for more information: -

https://firebase.google.com/docs/cloud-messaging/android/receive

https://firebase.google.com/docs/cloud-messaging/concept-options#notifications_and_data_messages

0
source

Source: https://habr.com/ru/post/1257462/


All Articles