Erik Erik - 6 months ago 51
iOS Question

APNS with Firebase version 3

I've set up Firebase to send push notifications to users of my app. However, I'm not sure what's best practice regarding user-to-user communication, such as chat clients. Should I get the device token at each startup of the app and use that for sending, or should I create a new topic for each "chatroom" that both parties are required to subscribe to? For instance when accepting a chat request.

If the first option is the best, how does this work? If the device is assigned a new token upon app startup, how can I be sure a given ID points to a specific device? The whole concept seems fragile - but could someone guide me to the most efficient solution?

I'm only looking to send chat messages / chat invites with push

Answer

Depending on the size and the privacy of the chatroom you can choose between using:

  • topics made for big groups, and without protections on joining / leaving
  • device tokens that you need to store in your server implementation.
    On the plus side they allow you to control the who is receiving the messages, and to send messages to individual device.

The device token does not change every startup. It is created when the application is launched for the first time, and can be updated in special cases. When the token change (again, this is rare) the FirebaseInstanceIdService.onTokenRefresh() callback is called.

In a generic chat application you might want to:

  1. first authenticate the user with your login system
  2. upload to your server the mapping user-id > device-token
  3. send messages to the users via the FCM server-side APIs.