Android 如何仅在选中CheckBoxPreference时推送firebase通知?
我使用Firebase通知是为了通知用户发布到应用程序的新数据。我遵循这里给出的示例: 尽管如此,我还是向用户提供了偏好。只有当他们选择通过选中Android 如何仅在选中CheckBoxPreference时推送firebase通知?,android,firebase,android-notifications,firebase-notifications,Android,Firebase,Android Notifications,Firebase Notifications,我使用Firebase通知是为了通知用户发布到应用程序的新数据。我遵循这里给出的示例: 尽管如此,我还是向用户提供了偏好。只有当他们选择通过选中CheckBoxPreference获得通知时,才应该得到通知 我已经成功地设置了CheckBoxPreference和onPreferenceChangeListener。问题是,即使未选中复选框,用户也会收到通知 以下是我在设置活动中所做的工作: public class SettingsActivity extends PreferenceActi
CheckBoxPreference
获得通知时,才应该得到通知
我已经成功地设置了CheckBoxPreference
和onPreferenceChangeListener
。问题是,即使未选中复选框
,用户也会收到通知
以下是我在设置活动中所做的工作:
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
来获得通知
请告诉我我做错了什么。当您使用Firebase通知时,发送的消息是通知消息。当应用程序位于前台时,您的代码将处理通知。但是当应用程序是后台的时候,默认的通知中心会处理它。由于通知中心对您的复选框一无所知,它将始终显示通知
有关到目前为止我所看到的最佳解释,请参见快速启动回购协议问题的答案:
您有几个选项可以让您的逻辑正常工作:
- 如果用户未选中此框,则不发送通知。因此,与其抑制显示,不如干脆不发送它。例如,您可能会发送到只有选择订阅的用户才能订阅的主题
- 发送数据消息而不是通知。这确保了您的代码将始终被调用,即使应用程序是后台的。有关通知和数据消息之间区别的更多信息,请参阅上的文档
相关问题:
您从哪里发送通知?控制台?@Shubhank我没听懂你的话。很抱歉请再说清楚一点,“我使用通知是为了通知用户”,那么您将如何通知用户。我猜是使用自定义服务器还是Firebaseconsole@Shubhank使用这个:非常感谢你消除了疑问。现在,你能告诉我如何发送数据消息而不是通知吗?再次感谢!