2

I am implementing push notification in my app after having it for IOS for a while.

Use case: I have user login, and accounts stored on our servers. I only want to send push notifications when a user is logged in, and only for the user currently logged in. Notifications are targeted to the individual user.

To do this, i fetch the token using

FirebaseInstanceId.getInstance().getToken()

, send push token to our server when user logs in, and remove it on logout.

Everything works as far as registering token, sending the token etc., HOWEVER, there could be scenarios where this doesn't work, for example if a user logs out with flight mode, so our server still has the token and thinks it should still send them.

On IOS, there are two local functions, register/unregisterforremotenotifications, that basically turns notifications on/off, regardless of whether my server could be contacted. I can call these on login/logout, and IOS won't show any remote notifications for my app, and i'm safe.

However, with Firebase, i can send the token to the server on login - for logout, however, its more complex since there's no "local" system-function to call that i can find.

The best thing i've figured out, is to always send a 'Data' notification, as described in this question, so that my notification service always gets called, even in the background, and there check if i am logged in, and not show the notification if i'm not.

However, the notification for the wrong user will still be sent to the phone, and it's a risk, for example if i, god forbid would have a bug... or the notification gets logged somewhere in the system.

Sooo, my question is if there's any way to disable notifications on logout via Firebase?

I hope this makes sense, thoughts much appreciated!

Mathias
  • 3,879
  • 5
  • 36
  • 48

2 Answers2

0

Yes, what you do is you create your own Login and Logout APIs. In your Login you should be storing your Token for PUSH notifications.

then whatever data triggers need to PUSH do a loop and build a push for known registered tokens.

On logout, simply delete that token from your Database and the loop will no longer include it for PUSH. You are correct using DATA tag will only work in foreground, but could be for wrong user that is correct.

If you do not have a backend, then please provide more clarity as to where/how you are storing and using your tokens to PUSH so I can help you further.

I have done this exact scenario to avoid wrong person getting push, but I support foreground and background on my scenario, so only logout or expired token will disable the PUSH on my app.

Sam
  • 5,342
  • 1
  • 23
  • 39
0

There are two ways:-

  1. Your app supports multi user login at the same time scenarios :- You can probably store the device token in share preferences with a boolean flag , when user relaunch the app , check if the flag for deletion is set then you can try deregistering from the service on server. If it fails then again you can do the same , so it would be something like checking boolean flag for account deletion on every app launch to make sure it deregister.

  2. Your app supports single usr:- You can simply delete firebase instance Id before signing into the account. This would take care of the scenario where a different user had signed out offline and you were not able to de register from your service.

You can also handle scenarios when to show notification based on accountId or user Id of the signed in user.

aurilio
  • 102
  • 6