2

I have a strange situation with the shared preferences in my application. I am using Firebase messaging service and I would like to set a boolean value to true in shared preferences whenever I recieve a message.

here's a simple code I worte:

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
getApplicationContext().getSharedPreferences(ApplicationConstants.SHARED_PREFRENCES_KEY, MODE_PRIVATE).edit().putBoolean(getString(R.string.badge_settings_key), true).apply();
}

I access this boolean value later in one of my activities. As you can see I set true for the boolean value in this method.

The strange thing is that when my application is in forground(one of the activites are visible), this works perfectly and whenever I access this value it is still true. But when my app is in background and I recieve a message, the value remains false and will not change. Either the service won't trigger correctly or the shared preferences file will not save correctly.

What can I do to solve this?

roostaamir
  • 1,928
  • 5
  • 24
  • 51
  • what if the user never touches the notificaion and launches the app directly? what can I do for this senario? – roostaamir Aug 18 '16 at 05:12
  • Then there is no need of notification in first place. – Sunil Sunny Aug 18 '16 at 05:14
  • For my application senario, there is. I want to show the user a notification that something has happend AND set that boolean value in shared preferences – roostaamir Aug 18 '16 at 05:21
  • In that case the issue is, on background your getApplicationContext() is null and the line where you save to preference will not work.Check if the application is visible and if not start activity using intent . – Sunil Sunny Aug 18 '16 at 05:34
  • getApplicationContext() is not null, Check my answer below. – Hisham Muneer Aug 18 '16 at 11:15
  • how did you solve this issue. I am also facing same situation. – Saneesh Apr 06 '17 at 05:28
  • @Saneesh The final solution was that I didn't use the "notification" key in the posted JSON. What I did was to send everything in "data" key and create the notification manually. The problem with the previous approach, as ` Frank van Puffelen` said, was that when the app is in the background and you set a "notification" key, the `onMeesageRecieved` method is never called, hence causing the issue – roostaamir Apr 06 '17 at 18:36
  • @roostaamir I have solved the issue by passing the data to the launcher activity and save it to the shared preferences . working fine in both background and foreground. I also use notification key to send the values – Saneesh Apr 07 '17 at 04:13

1 Answers1

2

onMessageReceived will not get called if you app is in background or killed and the notification is sent from Firebase Console.

What happened in your case, you have received the notification but the method onMessageReceivedwas never called and SharedPref is giving you false as default value of any boolean variable.

Solution: Don't sent Notification from Firebase Console, use their API.

Check this out.

Edit:

You must not put json key "notification" in the request to make it land in the onMessageReceived in all three scenarios of the app: Foreground, background and killed.

Community
  • 1
  • 1
Hisham Muneer
  • 8,558
  • 10
  • 54
  • 79
  • I am using their api and I am using their POST webservice to send a message. I have both data and notification in my message.what should I do to acheive my goal? (I want both the notification and the `onMessageRecieve` with the `data`) – roostaamir Aug 18 '16 at 11:25
  • 1
    In order to make it work: you must not put json key "notification" in your request to firebase api but instead use "data". Hard luck. – Hisham Muneer Aug 18 '16 at 11:28
  • but I need the `notification` key in order for it to,you know, notify the user that soemthig has happened and I need the data for internal jobs. what should I do if I want both? – roostaamir Aug 18 '16 at 11:33
  • Ditch the `notification` key, send everything in `data` and extract the message from there and create your own notification and show it to user. Also you must be getting notification icon issue when sending `notification` key, it will also solve that. – Hisham Muneer Aug 18 '16 at 11:42
  • When your app is backgrounded, notification messages are handled by the system and your `onMessageReceived()` will *not* be invoked. See the text after the JSON block in [this documentation page](https://firebase.google.com/docs/cloud-messaging/concept-options#notifications) and the first bullet point in [this documentation page](https://firebase.google.com/docs/cloud-messaging/android/receive#handling_messages). – Frank van Puffelen Aug 18 '16 at 13:29