10

I have implemented Firebase cloud messaging in my app for push notifications. Everything working fine. But notifications are getting even user logged out from app.

I heard that, I need to delete the FCM token when user logged out from app.

So I did like this in logout method:

-(void)logout{
        [[FIRInstanceID instanceID] deleteIDWithHandler:^(NSError *error){
            NSLog(@"%@",error);
        }];
}

But this is not working and next time when user logged in [[FIRInstanceID instanceID]token] is coming as null.

How to do this?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Himanth
  • 2,381
  • 3
  • 28
  • 41

1 Answers1

4

You can delete the token, but it's better to simply cancel the subscription. How to do that, depends on how you subscribed to the updates in the first place.

If you subscribed to a topic, you'll want unsubscribe when the user signs out.

AL.
  • 36,815
  • 10
  • 142
  • 281
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • I am subscribing to topics like this [[FIRMessaging messaging] subscribeToTopic:@"/topics/global"]; So when user signs out I have to do like this [[FIRMessaging messaging] unsubscribeFromTopic:@"/topics/global"]; right? – Himanth Dec 07 '16 at 09:55
  • Yup, that would be the way to unsubscribe from the same topic. – Frank van Puffelen Dec 07 '16 at 10:09
  • but I can see this is for the FCM messaging. Is this same for FCM notifications also? – Himanth Dec 07 '16 at 10:16
  • Yes, Firebase Notifications is essentially just a wrapper around Firebase Cloud Messaging. But since you mentioned FCM in your original post, I liked only to the docs for FCM. I suggest that you just give it a spin and report back if you're still having problems? – Frank van Puffelen Dec 07 '16 at 10:23
  • 3
    @himanth It's not advisable for you to delete the token every time the user logs out. See [here](http://stackoverflow.com/a/40589815/4625829). – AL. Dec 07 '16 at 11:06