1

I have already created a push notification system using firebase. It generates and saves a token for a user, then upon login displays their subscription status. It works fine, unfortunately it’s only one device per user, the most recent device they logged in on. I’d like to allow for multiple devices per user.

I’m assuming firebase uses some ID unique to each device to generate a token. If I’m wrong in that assumption, please point me in the right direction.

inktCode
  • 13
  • 6
  • 1
    "*I’d like to allow for multiple devices per user.*" - that's fully under your control. Just buid your system to remember each device token for that user. This is a very common implementation path. Don't use the token to identify the user - use the token to identify the app on the device. – Doug Stevenson Aug 24 '21 at 20:16
  • That’s how it currently is, the token is associated with the user in my DB. I want to associate a token with the device the current user is signed in with. That way I can display the subscription status for that particular device. – inktCode Aug 24 '21 at 21:52
  • You can't assign yourself a token, nor duplicate the logic that FCM uses to generate the tokens (which can change over time for a given device). All you can do is respond to the ones you get from the API. – Doug Stevenson Aug 24 '21 at 22:00
  • Dupicating FCM's token generation logic is exactly what I was hoping to acomplish, it's unfortunate I cannot. Thanks for your help. – inktCode Aug 25 '21 at 12:12

1 Answers1

1

As Doug commented, since FCM doesn't associate its tokens with users, this is probably some limitation in your implementation.

You'll want to allow multiple ID tokens per user in your database, and then send to all tokens for the current user. If the device/app install can be shared between users, you'll want to remove the association between the user and the token for that installation when the user signs out/a new user signs in.

On associating tokens with users, see:

And then finally you'll also want to clean up any tokens that FCM flags as not valid anymore, as otherwise you'll keep adding more and more tokens, which may not be valid anymore.

On deleting expired tokens, see:

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • So firebase doesn’t associate a token with a device? – inktCode Aug 24 '21 at 21:47
  • I’m not just looking to allow multiple tokens per user, I want to display the subscription status for the device the user is currently logged in with. – inktCode Aug 24 '21 at 21:55
  • A token is associated with an app instance on a specific device. So if 5 apps on one device use FCM that is 5 unique tokens, and if one app uses FCM and is on 5 devices, that is also 5 unique tokens. So you can use the FCM token to target a specific app in a specific device, but any association to a user will have to come from your own code. – Frank van Puffelen Aug 24 '21 at 22:20
  • You didnt solve my issue, I found my conversation with Doug very helpful though. – inktCode Sep 13 '21 at 15:46