应用关闭时,SharedPreferences在FirebaseMessageService中不起作用

时间:2016-09-21 17:03:45

标签: android firebase-cloud-messaging firebase-notifications

当应用关闭时,我已尝试将PreferenceManagers默认SharedPreferences放到我的FirebaseMessageService.onMessageReceived,但我无法让它工作。我似乎无法找出为什么这是不可能的。

我希望用户能够关闭来自我的设置面板的通知,其中有一个接收通知的开关。

SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this)
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
if (sharedPreferences.getBoolean("receive_notifications", true) == true) {
   notificationManager.notify(0, notifiBuilder.build());
}

无论设置是什么,它都将使用默认值true。

如果用户在应用中,一切都很完美,但在关闭时则不然。

1 个答案:

答案 0 :(得分:0)

使用getApplicationContext()代替。

SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext())