3

I am handling FCM notifications in my app. in like

public void onMessageReceived(RemoteMessage remoteMessage) {

        Notification notification = new Notification();
        notification.setTitle(remoteMessage.getNotification().getTitle());
        notification.setDescription(remoteMessage.getNotification().getBody());
        DateFormat simpledateFormat = new SimpleDateFormat("dd/MM/yyyy");
        DateFormat simpleTimeFormat = new SimpleDateFormat("hh:mm");
        Date date = new Date(remoteMessage.getSentTime());

        notification.setTime(simpleTimeFormat.format(date));
        Date date2 = new Date();
        notification.setDate(simpledateFormat.format(date2));
        DatabaseHelper databaseHelper = new    DatabaseHelper(getApplicationContext());
        databaseHelper.saveNotification(notification);

        //Calling method to generate notification
        sendNotification(remoteMessage.getNotification().getBody());
    }

but when app is not running or in background mode i am not able to save those notifications.

i am using "Regular activity" for processing and display of notifications in app.

as said in this link

don't know what i am missing.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Rahul
  • 337
  • 3
  • 10
  • http://stackoverflow.com/questions/37711082/how-to-handle-notification-when-app-in-background-in-firebase/ – Shubhank Jun 09 '16 at 06:21
  • Thanks Shubhank. but i gone through other posts and answer all are saying click_action in payload can not be set now in firebase. what other option could i use or will it work if i use my APP server. – Rahul Jun 09 '16 at 08:26
  • you need to use curl in command line or send a http request through a server or http request software for now to test custom click_action – Shubhank Jun 09 '16 at 08:30
  • Autharization key is same as api_key or it is client id from google-services.json for curl request – Rahul Jun 09 '16 at 09:03
  • I'm not quite sure what your issue is here, is onMessageReceived being called but your "saveNotification" call is failing, or, are you not getting the onMessageReceived call at all? Also what Notification class are you using? Also please include the request you are using to send the notification, that usually helps a log in figuring out what is going wrong on the client side. – Arthur Thompson Jun 17 '16 at 13:52
  • when app is in background onMsgRecevd not getting called. i got solution user_action need to be set for the msgs – Rahul Jun 19 '16 at 04:33

1 Answers1

2

Firebase notifications in Android are not sent to your registered messages service when your app is in background and your notification contains a notification key. To ensure your service always handles notifications, don't use the notification field. Instead use the data field for that purpose.

Actually one downside of using Firebase's notification field is that you cannot specify a large and small icons for the system tray notification. It only lets you set one icon.

Note however that this is not a "universal" solution since iOS users will not receive a notification in background when there's no notification field present.

Having said that, I think the best solution right now is to send you Android users a notification like the following:

{
    "to":"tHt4D...",
    "data":
    {
        "title":"Nice title",
        "body":"Nice body"
    }
}

and handle it in your service like this:

public void onMessageReceived(RemoteMessage remoteMessage) {
    Map<String, String> data = remoteMessage.getData();
    makeMyCustomNotification(data.get("title"), data.get("body"));
}

For iOS users use the standard way:

{
    "to":"tHt4D...",
    "notification":
    {
        "title":"Nice title",
        "body":"Nice body"
    }
}

There's a big discussion around this topic here

Gonzalo
  • 3,674
  • 2
  • 26
  • 28
  • What happens when an app in background state, while data payload message sent. Does the 'onMessageReceived' method called from service? – Thamarai T Oct 30 '19 at 04:50
  • From what I remember, it should get called, but I no longer work with Firebase since I changed job. I would recommend performing all necessary tests yourself to get an overall feeling of how it works. Besides, it's been more than 3 years since my reply and things might have changed by now. – Gonzalo Oct 30 '19 at 13:54
  • 1
    I checked and it's working.. 'onMessageReceived' method is called when an app in background state for data payload message. – Thamarai T Oct 30 '19 at 14:26