我正在使用Firebase通知,以便通知用户有关发布到应用的新数据。我按照此处给出的示例进行操作:https://github.com/firebase/quickstart-android/tree/master/messaging
虽然,我也向用户提供偏好。只有当他们选择通过查看CheckBoxPreference
来获得通知时,才会收到通知。
我已成功设置CheckBoxPreference
和onPreferenceChangeListener
。问题是,即使未选中CheckBox
,用户也会收到通知。
这是我在SettingsActivity
中所做的事情:
public class SettingsActivity extends PreferenceActivity {
private AppCompatDelegate mDelegate;
Preference myPref;
boolean isChecked = true;
private static final String TAG = "SettingsActivity";
public static final String RECEIVE_NOTIFS = "receiveNotifications";
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
getDelegate().installViewFactory();
getDelegate().onCreate(savedInstanceState);
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
myPref = (CheckBoxPreference) findPreference(RECEIVE_NOTIFS);
myPref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object o) {
isChecked = Boolean.valueOf(o.toString());
if (isChecked) {
Toast.makeText(getBaseContext(), "Checked", Toast.LENGTH_SHORT).show();
if (getIntent().getExtras() != null) {
String remoteMessage = getIntent().getExtras().getString("remoteMessage");
sendNotification(remoteMessage);
} else {
Toast.makeText(getBaseContext(), "Error!", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(getBaseContext(), "Unchecked", Toast.LENGTH_SHORT).show();
}
return true;
}
});
}
public void sendNotification(String messageBody) {
Intent intent = new Intent(this, SettingsActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("FCM Message")
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
我已在上面的代码中明确指出,只有在选中CheckBoxPreference
时才会发送通知。
此处的MyFirebaseMessagingService.java
文件代码:
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "MyFirebaseMsgService";
/**
* Called when message is received.
*
* @param remoteMessage Object representing the message received from Firebase Cloud Messaging.
*/
// [START receive_message]
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// TODO(developer): Handle FCM messages here.
// If the application is in the foreground handle both data and notification messages here.
// Also if you intend on generating your own notifications as a result of a received FCM
// message, here is where that should be initiated. See sendNotification method below.
Log.d(TAG, "From: " + remoteMessage.getFrom());
Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());
Intent intent = new Intent(this, SettingsActivity.class);
intent.putExtra("remoteMessage", remoteMessage.getNotification().getBody());
startActivity(intent);
}
// [END receive_message]
}
我在这里做的是将remoteMessage
作为意图发送到SettingsActivity
并在sendNotification()
方法中使用它。
所以,显然我想要的是我希望用户只有在选中CheckBoxPreference
时才会收到通知。
请告诉我这里我做错了什么。
答案 0 :(得分:2)
使用Firebase通知时,发送的消息是通知消息。当应用程序位于前台时,您的代码将处理通知。但是当应用程序背景化时,默认通知中心将处理它。由于通知中心对您的复选框一无所知,因此它将始终显示通知。
有关快速启动回购的问题,请参阅此答案,以获得我迄今为止所见的最佳解释:https://github.com/firebase/quickstart-android/issues/8#issuecomment-221039175
您可以使用几个选项来使逻辑正常工作:
不发送通知。因此,不要压制显示,只是不要发送它。例如,您可能会发送到仅选择订阅的用户的主题。
发送数据消息而不是通知。这样可以确保始终调用代码,即使应用程序背景也是如此。有关通知和数据消息之间差异的更多信息,请参阅Notifications and data in the message payload上的文档。
相关问题: