0

I am using Firebase messaging to send notifications. What I want is if the user has logged in into the app only then she must get the notifications. But the notifications starts pouring the moment user downloads the app irrespective of whether the user has logged in into the app or not. How to handle this situation?

I didn't get anything helpful from the internet. Any help is appreciated.

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);
        }
    }
digital_pro
  • 183
  • 2
  • 13
  • You need to send `Data Message` notification follow this [answer](https://stackoverflow.com/questions/37711082/how-to-handle-notification-when-app-in-background-in-firebase) and check if user is logged in `onMessageReceived()` if he is logged in then display notification. – Mohammed Farhan Apr 04 '18 at 05:05
  • If you are maintaining session of user i.e by sharedprefs(local) and in onMessageReceived you can easily check whether a user is logged in or not and if you are maintaining user session on server and server can easily check whether user is online or not(only when your server is sending push notifications to user) not in case of user sending notifications to other user via REST API – yashkal Apr 04 '18 at 05:11
  • @Lucky I used shared pref to keep a track if the user has logged in or not but still not working. See the edited code. – digital_pro Apr 07 '18 at 05:12

1 Answers1

1

when user logged in save user name and password in shared preference and retrive the saved username and password in onMessageReceived like below

@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"));
    }
}
Fazal Hussain
  • 1,129
  • 12
  • 28
  • I used shared pref to keep a track if the user has logged in or not but still not working. See the edited code. – digital_pro Apr 07 '18 at 05:14