1

I am using Firebase for sending push notification to my app and want to save some values to sharedpreferences. Its working perfectly when the app is foreground but it is not saving the values when the app is in background. My code looks like this:

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        Log.e("Remotemessage",remoteMessage.toString());

        String Heading = remoteMessage.getNotification().getTitle();
        String message = remoteMessage.getNotification().getBody();
        Map<String, String> data = remoteMessage.getData();
        Log.e("Firebase notification ", " KEY1");
        sessionManager=new SessionManager(this);

        Log.e("Firebase notification ", " KEY"+data.get("click_action"));
        Log.e("Firebase notification ", " KEY new"+data.get("key"));
        int studentid= Integer.parseInt(data.get("student_id"));
        Log.e("STUDENT ID",""+studentid);
        if(studentid!=0)
        {
            sessionManager.saveStudentId(studentid);

        } 
}

but notification is showing both cases. How to solve this issue??

AL.
  • 36,815
  • 10
  • 142
  • 281
Saneesh
  • 1,796
  • 16
  • 23
  • The documentation also mentions the following for receiving a notification when the app is in background: 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. – Ratilal Chopda Apr 06 '17 at 05:55
  • `replace sessionManager=new SessionManager(this);` with `sessionManager=new SessionManager(getBaseContext());` – Pragnesh Ghoda シ Apr 06 '17 at 06:26
  • Use only data field while sending notification from your server. – Mayura Devani Apr 06 '17 at 06:29
  • Thanks @RatilalChopda it worked . – Saneesh Apr 06 '17 at 06:53
  • Welcome... @Saneesh – Ratilal Chopda Apr 06 '17 at 06:58
  • Please consider case: What if user don't tap the notification and just clear the notification? In this case, intent of your launcher Activity won't be called and so It wont save your sharedpreferences data. – Mayura Devani Apr 06 '17 at 07:32
  • @MayuraDevani Like I replied in the comments section on my answer, this is decided by the *user*. You can't force them to tap on the notification which is needed. If the use-case is that the data is of *utmost importance* to the app, it is advisable to have an implementation of getting data from your server (requesting it) to send to your app on start-up. – AL. Apr 06 '17 at 07:35
  • That is exactly the use case what i wanted you guys to consider. – Mayura Devani Apr 06 '17 at 07:38
  • @MayuraDevani you have a point. have you found any solution ?? – Saneesh Apr 06 '17 at 07:51
  • @Saneesh If you're app actually needs the data you want to save, have an implementation in your app that retrieves it from your App Server. Push notifications should only be used to *improve* user experience. Not to send *critical data*. – AL. Apr 06 '17 at 07:57
  • @AL. yes I understand that sir. – Saneesh Apr 06 '17 at 08:07
  • If you have a database (something like [Firebase Database](https://firebase.google.com/docs/database/)) where you can get that data from, you should get it directly (usually on App Start). – AL. Apr 06 '17 at 08:08
  • Sir I have a scenario of two student having two student_id. so when the notification is send I have to specify the notification is for which student. for that I have to pass the student id with the notification. because of that I need to store the data to sharedpreferences. @AL. – Saneesh Apr 06 '17 at 08:26
  • "*a scenario of two student having two student_id*" -- so one for each? Sorry, it's a little confusing. Each FCM registration token is unique for each *app instance*. That itself is (usually) the used identifier for a user's device. – AL. Apr 06 '17 at 08:32
  • Its a School app for parents . the two students means siblings. I want to specify the notification is for which student. – Saneesh Apr 06 '17 at 08:47
  • As i replied earlier, Solution is: Use only data field while sending notification from your server. Then always onMessageReceived will be called. Check document: https://firebase.google.com/docs/cloud-messaging/android/receive – Mayura Devani Apr 06 '17 at 09:10

1 Answers1

1

Basing from your post, I'll presume that you are sending a payload that contains both notification and data messages. See Message Types for more details.

As can be seen in the Handling Messages for Android documentation, when sending a payload that contains both message type while the the app is in background, the message (notification data) will be handled by the Android System Tray.

There are two things you could do:

  1. Send a data-only message payload, so that onMessageReceived() will be handling the message regardless if your app is in foreground or background.

  2. Implement the handling in getting the details in your data payload after the user taps on the notification in the Android system tray. See my answer here.

Community
  • 1
  • 1
AL.
  • 36,815
  • 10
  • 142
  • 281
  • What if user don't tap the notification and just clear the notification? – Mayura Devani Apr 06 '17 at 07:31
  • @MayuraDevani Then it won't proceed to the activity. That itself is *the user's choice*. – AL. Apr 06 '17 at 07:33
  • Yes, your information is right but Saving data in shared preferences won't work in @Saneness's case – Mayura Devani Apr 06 '17 at 07:34
  • That's right. But the question is about the FCM message not being received when in background. – AL. Apr 06 '17 at 07:37
  • In question, he said: Its working perfectly when the app is foreground but it is not saving the values when the app is in background. So he must want to save important data in shared preferences. – Mayura Devani Apr 06 '17 at 07:39
  • Yes. So the issue is the message is not being handled properly when the app is in the background. And if he does mention that the data is *highly important*, I would suggest using a different way instead of using push notification. – AL. Apr 06 '17 at 07:56