Android 不得向未登录的用户发送通知
我正在使用Firebase消息发送通知。我想要的是,如果用户只登录到应用程序,那么她必须得到通知。但是,无论用户是否登录应用程序,只要用户下载应用程序,通知就会开始蜂拥而至。如何处理这种情况 我没有从互联网上得到任何帮助。感谢您的帮助 MyFirebaseMessagingService.javaAndroid 不得向未登录的用户发送通知,android,firebase,firebase-cloud-messaging,Android,Firebase,Firebase Cloud Messaging,我正在使用Firebase消息发送通知。我想要的是,如果用户只登录到应用程序,那么她必须得到通知。但是,无论用户是否登录应用程序,只要用户下载应用程序,通知就会开始蜂拥而至。如何处理这种情况 我没有从互联网上得到任何帮助。感谢您的帮助 MyFirebaseMessagingService.java FirebaseUser firebaseUser; private FirebaseAuth mAuth=FirebaseAuth.getInstance(); @Over
FirebaseUser firebaseUser;
private FirebaseAuth mAuth=FirebaseAuth.getInstance();
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
String notificationTitle = null, notificationBody = null;
//shared preference to check if the user has logged in or not
SharedPreferences preferences=getSharedPreferences(LOGIN_PREF_NAME,MODE_PRIVATE);
String login_status=preferences.getString("accepted",null);
if (remoteMessage.getNotification() != null) {
Log.d(REGTAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
notificationTitle = remoteMessage.getNotification().getTitle();
notificationBody = remoteMessage.getNotification().getBody();
}
mAuthListener=new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
if (firebaseAuth.getCurrentUser() != null && !TextUtils.isEmpty(login_status) && login_status.equals("true")) {
PendingIntent pendingIntent=PendingIntent.getActivity(MyFirebaseMessagingService.this, INT, intent,
PendingIntent.FLAG_ONE_SHOT);
final NotificationCompat.Builder notificationBuilder;
notificationBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(this)
.setAutoCancel(true) //Automatically delete the notification
.setSmallIcon(R.mipmap.ic_launcher) //Notification icon
.setContentIntent(pendingIntent)
.setContentTitle(notificationTitle)
.setContentText(notificationBody);
mAuth = FirebaseAuth.getInstance();
firebaseUser=mAuth.getCurrentUser();
mAuthListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
if (firebaseUser != null ) {
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(INT, notificationBuilder.build());
}
}
};
mAuth.addAuthStateListener(mAuthListener);
}
}
当用户登录时,在共享首选项中保存用户名和密码,并在onMessageReceived中检索保存的用户名和密码,如下所示
@Override
public void onMessageReceived(String from, Bundle data) {
utils = new Utils(this);
if (firebaseAuth.getCurrentUser() != null && login_status != null && login_status.equals("true")) {
sendNotification1(data.getString("message"),data.getString("direction"),data.getString("title"));
}
}
您需要发送
数据消息
通知,按照此操作,检查用户是否登录onMessageReceived()
如果用户已登录,则显示通知。如果您正在通过sharedprefs(本地)维护用户会话在onMessageReceived中,您可以轻松检查用户是否登录,以及是否在服务器上维护用户会话,并且服务器可以轻松检查用户是否在线(仅当服务器向用户发送推送通知时)不适用于用户通过REST向其他用户发送通知的情况API@Lucky我使用shared pref跟踪用户是否已登录但仍不工作。查看已编辑的代码。我使用shared pref跟踪用户是否已登录但仍不工作。请参阅编辑的代码。