我正在使用 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
作为 Intent 发送到SettingsActivity
并在那里的方法中使用它sendNotification()
。
所以,很明显我想要的是我希望用户只有在他们选择通过检查CheckBoxPreference
.
请让我在这里做错了什么。