Skip to content Skip to sidebar Skip to footer

Save Application Wide Boolean In Sharedpreference

I know that this is a common question asked, and I have spent all afternoon trying different solutions that don't seem to work. I am trying to store a boolean receiveNotifications

Solution 1:

A push message is a Json object, next example is directly from the docs:

{"message":{"token":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...","notification":{"title":"Portugal vs. Denmark","body":"great match!"}}}

There are 3 types of push messages, notification, data, and both;

//Notification"message":{"notification":{}}//data"message":{"data":{}}//both"message":{"notification":{},"data":{}}

Each will trigger a different behavior in the app depending if the app is open or not.

  • Notification: if the app is open the code on the service will be executed, if not the notification is showed by default

  • Data: Always the code on the service will be executed

  • Both: f the app is open the code on the service will be executed, if not the notification is showed by default and the data will be available in the launcher activity as extra obtainable from the intent

The Firebase web console will always send "notification" type and if you add data as custom params it will send both.

Your boolean will never be taken in consideration if the app is closed and the notification comes from the web console.

Solution 2:

Turns out that no matter what you do Firebase must override whatever you have set in the application. I found this out by instead of sending from the Firebase console, I sent notification from my web server. The notification was stopped perfectly and the Shared Preference worked.

Solution 3:

 private SharedPreferences prefs;
 private SharedPreferences.Editor editor;


 prefs = getApplicationContext().getSharedPreferences("notifiactions", 
 MODE_PRIVATE);

 editor = prefs.edit();

 /////Assigning a Boolean///////////

editor.putBoolean("receiveNotifications", false);
editor.commit();

//Retrieving Boolean
 prefs = getApplicationContext().getSharedPreferences("notifications", 
 MODE_PRIVATE);
 bool = prefs.getBoolean("receiveNotifications", true);

   //Try replacing this with your code also
     if(bool){

     }

Post a Comment for "Save Application Wide Boolean In Sharedpreference"