我正在使用firebase消息发送通知。我想要的是,如果用户仅登录到该应用程序,那么她必须收到通知。但是,无论用户是否已登录应用程序,通知开始倾注用户下载应用程序的时刻。如何处理这种情况?
我没有从互联网上获得任何帮助。任何帮助都将不胜感激。
myfirebasemessagingservice.java
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);
}
}
当用户登录"保存用户名"one_answers"共享"偏好中的用户名和密码时
@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"));
}
}