3

I know that there is the same question with this title but unfortunately it is not answered right and it is accepted!!! here

I want to know how I can find out a FCM message received when app is in background to do some action on message received before clicking by user. but when app is in background onMessageReceived is not triggered!

I googled so much and could not find a good way.

Community
  • 1
  • 1
Mahdi
  • 6,139
  • 9
  • 57
  • 109
  • [Android Push Notifications using Firebase](http://www.androidhive.info/2012/10/android-push-notifications-using-google-cloud-messaging-gcm-php-and-mysql/) use this tutorial. i have used this in my app and it works well. – faraz khonsari Feb 27 '17 at 07:30
  • http://stackoverflow.com/a/37711151/3505534 – Ramesh sambu Feb 27 '17 at 07:31
  • You're probably using a `notification` message payload. When your app is in background, the Android Notification Tray will handle the push notification. Try using a `data`-only message payload, so that it will always be handled by `onMessageReceived()`. See https://firebase.google.com/docs/cloud-messaging/android/receive#handling_messages – AL. Feb 27 '17 at 07:43
  • 1
    @AL. you right. by removing notification part it is worked. – Mahdi Feb 27 '17 at 07:52
  • @farazkhonsari please read the question carefully. any way thanks for your comment. – Mahdi Feb 27 '17 at 07:54
  • Glad to hear it's working. Cheers! :) – AL. Feb 27 '17 at 07:58
  • @AL. do you want to write answer so I can accept it? – Mahdi Feb 27 '17 at 08:51
  • Okay. That'll be cool. :) – AL. Feb 27 '17 at 09:10

3 Answers3

6

To handle FCM push notification from onMessageReceived() when the app in background the server should always send Data only messages.

Notification messages can only handle when the app is in the foreground. When the app is in the background an automatically generated notification is displayed. When the user taps on the notification they are returned to the app. Messages containing both notification and data payloads are treated as notification messages.

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

  • Notification messages, sometimes thought of as "display messages."
  • Data messages, which are handled by the client app.

A notification message is the more lightweight option, with a 2KB limit and a predefined set of user-visible keys. Data messages let developers send up to 4KB of custom key-value pairs. Notification messages can contain an optional data payload, which is delivered when users tap on the notification.

Notification messages

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

Notification messages are delivered to the notification tray when the app is in the background. For apps in the foreground, messages are handled by these callbacks:

  • onMessageReceived() on Android. The notification key in the data bundle contains the notification.

Data messages

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

On Android, a client app receives a data message in onMessageReceived() and can handle the key-value pairs accordingly.

Note these further platform-specific details:

  • On Android, the data payload can be retrieved in the Intent used to launch your activity.

Messages with both notification and data payloads

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

App behavior when receiving messages that include both notification and data payloads depends on whether the app is in the background or the foreground—essentially, whether or not it is active at the time of receipt.

  • 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.

  • When in the foreground, your app receives a message object with both payloads available.

Reference

Anoop M Maddasseri
  • 10,213
  • 3
  • 52
  • 73
  • 1
    you writes the answer but one of user is tell this before you in comment. so I up-vote your answer and thanks and wait till he write the answer for accepting. – Mahdi Feb 27 '17 at 08:52
5

As I've mentioned in the comments section, when sending a message with a notification message payload the Android System (Notification Tray) will handle the push notification when your app is in background.

You should use a data-only message payload, so that it will always be handled by onMessageReceived().

See the Handling Messages docs in Android for more details.

AL.
  • 36,815
  • 10
  • 142
  • 281
  • thanks. Is there any way that when notification mode message opend by user pass notification title with boundle? – Mahdi Feb 27 '17 at 09:53
  • No. The only details you'll be able to get is the one included in the `data` payload. You'll have to include the same data you have in the `notification` inside your `data`. – AL. Feb 27 '17 at 10:15
  • how can handle message when app is closed? – Mahdi Mar 11 '17 at 13:52
0

Handle notification messages in a backgrounded app

When your app is in the background, Android directs notification messages to the system tray. A user tap on the notification opens the app launcher by default.

This includes messages that contain both notification and data payload (and all messages sent from the Notifications console). In these cases, 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.

Ajay Venugopal
  • 1,544
  • 1
  • 17
  • 30