Save Application Wide Boolean in SharedPreference

雨燕双飞 提交于 2019-12-25 03:18:05

问题


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 in SharedPreferences but when I send a notification it still comes through. When I check whether the boolean is set in the activity I set it in, it says that the value is what it should be, but when I call this in my Firebase MessagingService it still allows the notification to come through.

This is my first time using them so if you see the obvious answer thats why.

Storing the Boolean:

// shared preferences
notificationsPref = mContext.getSharedPreferences("notifications", MODE_PRIVATE);
SharedPreferences.Editor editor = notificationsPref.edit();
                editor.putBoolean("receiveNotifications", false);
                editor.apply();

Checking if Boolean is Set:

// check if they want to receive notifications
SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences("notifications", MODE_PRIVATE);
Boolean areNotificationsAllowed = sharedPreferences.getBoolean("receiveNotifications", true);
if (areNotificationsAllowed){
        Toast.makeText(this, "Send Notification", Toast.LENGTH_SHORT).show();
        sendNotification(contentTitle, messageBody);
}

回答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.




回答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.




回答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){

     }


来源:https://stackoverflow.com/questions/50337205/save-application-wide-boolean-in-sharedpreference

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!