-1

I have to show push notification while user is logged in.So when app opens am taking deviceToken.So it will register to server always.But when user logout i dont want to show push notification.So how can i do it?

Now i have done with checking preference value like the below code,

 public void onMessageReceived(String from, Bundle data) {
    loginPref = getSharedPreferences("loginPreference", Context.MODE_PRIVATE);
    mEditPrefs = loginPref.edit();
    userID = loginPref.getString("userId", null);
    if(userID!=null){
       Bundle bundle = data.getBundle("notification");
        Log.d("Bundle123::", bundle + "");
        if (bundle != null) {
            String text = bundle.getString("text");
            String body = bundle.getString("body");
            String title = bundle.getString("title");
            Log.d("text123::", text + "");
            Log.d("title123::", title + "");
            Intent in = new Intent();
            in.setAction("GCM_RECEIVED");
            sendBroadcast(in);
            sendNotification(title,body);
        }
    }

But when app backgrounds this condition doesn't work.How can i do it?Because i cant delete the token because when am trying login i have to send device token too..

Sunisha Guptan
  • 1,555
  • 17
  • 44
  • 1
    Possible duplicate of [Firebase Cloud Messaging - Handling logout](http://stackoverflow.com/questions/43193215/firebase-cloud-messaging-handling-logout) – AL. May 02 '17 at 10:13
  • You can simply disable the notifications by having some sort of setting that automatically turns false when the user logs out (if it's false, don't show the notification -- this is assuming that your app will be the only one to handle the notifcations). Other than that, you'll have to disable the notifications from your server or delete the token (as per the possible duplicate post). – AL. May 02 '17 at 10:14
  • How can i disable the notification? – Sunisha Guptan May 02 '17 at 10:19
  • @AL. its not duplicate one.Because i dont want to use deleteInstanceId.I ahve already mentioned in my question – Sunisha Guptan May 02 '17 at 10:21
  • I tagged it as a possible duplicate because the gist is the same. I've provided the other possible options in my following comment. You could disable *showing* the notification, but you can't entirely disable it from your app alone. Either you set a variable that you check in the SharedPrefs. If true (default), show the notification, else, don't show the notification. – AL. May 02 '17 at 10:28
  • Removing the *mapping* of the token seems like a good approach too. See [this answer](http://stackoverflow.com/a/37734219/4625829). – AL. May 02 '17 at 10:31
  • While logout time in back end side they done device token as null.And my problem has been solved. – Sunisha Guptan May 03 '17 at 05:13
  • Hi Sunisha. Cool. You should add in an answer. Other users might find it useful in the future. Cheers! – AL. May 03 '17 at 12:39
  • Yup. Thanx @AL. – Sunisha Guptan May 04 '17 at 04:18

1 Answers1

3

While logout time tell to back end side to do device token as null.

Sunisha Guptan
  • 1,555
  • 17
  • 44