【问题标题】:Save Application Wide Boolean in SharedPreference在 SharedPreference 中保存应用程序范围的布尔值
【发布时间】:2018-05-14 18:56:02
【问题描述】:

我知道这是一个常见的问题,我整个下午都在尝试似乎不起作用的不同解决方案。

我正在尝试在 SharedPreferences 中存储一个布尔值 receiveNotifications,但是当我发送通知时它仍然会通过。当我检查是否在我设置的活动中设置了布尔值时,它说该值是它应该是的值,但是当我在 FirebaseMessagingService 中调用它时,它仍然允许通知通过。

这是我第一次使用它们,所以如果您看到明显的答案,那就是原因。

存储布尔值:

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

检查是否设置了布尔值:

// 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);
}

【问题讨论】:

  • 你确定getApplicationContext() == mContext
  • 这可能是问题所在,mContext = MainActivity.this
  • 但是当我从 Firebase 控制台发送测试通知时,它仍然会将通知发送到我的设备
  • 我认为Context 在这里应该不是问题,因为getSharedPreferencesContextWrapper 的方法。 ActivityApplication 都一样。在收到通知时调试您的代码。

标签: java android sharedpreferences firebase-cloud-messaging


【解决方案1】:

推送消息是一个 Json 对象,下一个示例直接来自文档:

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

推送消息有3种类型,通知,数据,两者兼有;

  //Notification
  "message":{
    "notification":{
    }
  }

  //data
  "message":{
    "data":{
    }
  }

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

每个都会在应用中触发不同的行为,具体取决于应用是否打开。

  • 通知:如果应用打开,服务上的代码将被执行,否则默认显示通知

  • 数据:总是会执行服务上的代码

  • 两者:如果应用程序打开,则将执行服务上的代码,否则默认显示通知,并且数据将在启动器活动中可用,可从 Intent 中额外获得

Firebase 网络控制台将始终发送“通知”类型,如果您将数据添加为自定义参数,它将同时发送。

如果应用已关闭并且通知来自网络控制台,则永远不会考虑您的布尔值。

【讨论】:

    【解决方案2】:

    事实证明,无论您做什么Firebase 都必须覆盖您在应用程序中设置的任何内容。我发现这一点不是从 Firebase 控制台发送,而是从我的网络服务器发送通知。通知已完全停止,共享首选项有效。

    【讨论】:

      【解决方案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){
      
           }
      

      【讨论】:

      • 如果它不起作用..问题一定是firebase..如果应用程序崩溃或与firebase相关的代码,请发布您的logcat
      • 我修好了。代码运行良好,但它是 Firebase 控制台
      • 对..在编辑中指定它,以便其他人可以与之相关。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多