5

When in the background, apps receive the notification payload in the notification tray, and only handle the data payload when the user taps on the notification.

Is there any way to handle the data payload without user tapping on the notification?

AL.
  • 36,815
  • 10
  • 142
  • 281
Mridul S Kumar
  • 326
  • 1
  • 4
  • 20

2 Answers2

1

Basing from the FCM docs on Handling Messages:

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

It's not specifically handled ONLY by tapping on the Notification. You may handle it in the onMessageReceived(). Pretty sure the tap action also depends on how you implement it.

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

AL.
  • 36,815
  • 10
  • 142
  • 281
  • I want to store the data in SQLite – Mridul S Kumar Sep 29 '16 at 09:38
  • @MridulSKumar you can handle the data payload in the `onMessageReceived()`. Retrieve the details you need from their, then save it to the device SQLite using your usual implementation. – AL. Sep 29 '16 at 09:40
  • 1
    onMessageReceived() is not called when the app is in background. It works only when app is in foreground. – Mridul S Kumar Sep 29 '16 at 09:42
  • I see. When the app is in background (referring to the table above), the data payload can be retrieved ***Data: in extras of the intent.*** – AL. Sep 29 '16 at 09:45
  • To get the data in extras of the intent I guess you have to click on the Notification. What if the user clears the notification without clicking it ? – Mridul S Kumar Sep 29 '16 at 09:47
  • I'm referring to the both column in the above table – Mridul S Kumar Sep 29 '16 at 09:49
  • @MridulSKumar If you are only using the data payload, then it should call `onMessageReceived()`. Unless you're using both notification and data in your payload, the behavior is as you would see in the table. Why the downvote? – AL. Sep 29 '16 at 10:44
  • @MridulSKumar I see. Anyways, as I mentioned in the last comment, do try to only use data payload and see if you are able to handle it via the `onMessageReceived()`. – AL. Sep 29 '16 at 10:50
  • In case of multiple notifications, *extras of the intent* will overwrite with the last notification arrived. any solution? – Mohsin Jul 20 '17 at 06:24
0

If you want to receive data payload when app is not running then you need to use a custom app-server with http protocol(in your case) which will send only data payload no notification and will send data to fcm endpoint like this

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

or just to send data from 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 downstream message to your device and it will work the way you want. If you want to have a basic idea of such app server please consider this : How to push notification to client when Firebase has a new entry?

I have posted basic Java Servlet code here and also a node.js code by firebase official blog.

Hope it helps.

Community
  • 1
  • 1
Nishant Dubey
  • 2,802
  • 1
  • 13
  • 18
  • I'm able to receive the data and store in SQLite when the app is in foreground. onMessageReceived() is not called when the app is in background. I'm sending data along with notification. So according to their docs when app is in background Notification is displayed in the system tray and data is handled in the extras of the intent. – Mridul S Kumar Sep 29 '16 at 09:59
  • @Nishant Dubey FCM Docs Clearly says that if App is in background onMessageReceived not called Please Read DOCS carefully – Mohit Trivedi Sep 29 '16 at 10:08
  • Hi Mohit. I do understand your concern . But downvoting the answer is not the way unless you check the **edit** – Nishant Dubey Sep 29 '16 at 10:13
  • @MridulSKumar check updated answer. Its the only solution using fcm – Nishant Dubey Sep 29 '16 at 10:15