2
votes

I use GCM push notification for my Android app which is basically an instant messenger like Line/What's App. In my app, even after the user logs out, the push notification continue to happen. IN other words, I have no control over the notifications.

Ideally, I would like the notifications to stop when the user logs out of the app and resume after he logs in. Thanks in advance.

4
You should share a bit how you implement the push notifications, What you have tried already to make them stop and didn't work. - Raanan

4 Answers

7
votes

You should use SharedPreferences.

You should have a preference that contain the value if the user should be alerted or not. Let's say, once a user logged out - set the preference value to false. Now,in your GCMIntentService in the onMessage method, check the value of the preference, if its false, just do nothing.

Have a look at this example:

How to use SharedPreferences in Android to store, fetch and edit values

5
votes

Call This Method GCMRegistrar.unregister(context); when the user signs out to stop receiving the push messages.

1
votes

You have to unregister your app to avoid receiving further push notifications. You can use GCMRegistrar.unregistrar() to make this happen.

0
votes

As Google unregister() method was deprecated, instead use the InstanceID.deleteInstanceID().