5

Android push notification using FCM is unable to handle when application in background. Default message is displayed in notification tray.

Can Anyone help me how to handle the messages when application in background.

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);

}

This method is not called when application is in background. Any help will be great help for me.

Raghul Sugathan
  • 350
  • 1
  • 7
  • 22
  • http://stackoverflow.com/a/42268901/7320259 check this – Zaki Pathan Feb 22 '17 at 10:46
  • 1
    That's how notification messages works ... As it is stated in documentation there are 2 messages types ... It is also written how they work. – Selvin Feb 22 '17 at 10:46
  • http://www.androidhive.info/2012/10/android-push-notifications-using-google-cloud-messaging-gcm-php-and-mysql/ try this it helps you for background notification – Zaki Pathan Feb 22 '17 at 10:47
  • androidhive is a bad tutorial source also it is from 2012 so it is obsoleted(as it is using GCM not FCM) – Selvin Feb 22 '17 at 10:49
  • @ZakiPathan Hello man.. your stackoverflow reference is good. But the issue is where I have to implement the logic which is mentioned in stackoverflow. Because I dont have any control over the code when app goes to background. – Raghul Sugathan Feb 22 '17 at 11:01
  • if (!NotificationUtils.isAppIsInBackground(getApplicationContext())) condition check for app in background or foreground – Zaki Pathan Feb 22 '17 at 11:03
  • @ZakiPathan I got what you are trying to say. My issue here is "onMessageReceived" block is not getting called when app is in backgorund. If the above method is called means your reference is good and will be implemented. Can You suggest why onMessageReceived method not getting called. – Raghul Sugathan Feb 22 '17 at 11:11
  • 1
    *My issue here is "onMessageReceived" block is not getting called when app is in backgorund* ... again that's how messages of type notification works **you cannot change it** , you need to send message of second type ... what is a second type and how to send it ? it is all in the FCM's documentation (and in thousands of similar questions, here on stackoverflow - internet search: "onmessagereceived not called") – Selvin Feb 22 '17 at 12:15

1 Answers1

3

The onMessageReceived() will be always called if you receive the payload as "data" always from server.

Example : Payload from server should be like following :

{
 "data":{
 "id": 1,
 "missedRequests": 5
 "addAnyDataHere": 123
 },
 "to":"fhiT7evmZk8:APA91bFJq7Tkly4BtLRXdYvqHno2vHCRkzpJT8QZy0TlIGs......"
}

for more about this issue. Please refer following URL : Firebase onMessageReceived not called when app in background

Ankit Mehta
  • 4,251
  • 4
  • 19
  • 27