1

I'm using Firebase's FCM for push notifications for an app. There is a need to subscribe to certain topics from the moment the user starts the app.

So far I've handled "topics" and various metrics on our own server, but as we're moving logic parts to their appropriate place to lessen the load on our server, this needs to be moved to Firebase itself.

And we want to handle things a bit differently. Users will be able to subscribe and unsubscribe to/from certain notification groups, however FCM's documentation does not mention which is the time, which callback is the proper place to handle this.

The flow would be the following:

  1. User installs app
  2. User launches app
  3. App updates cached data (information that changes in larger intervals, e.g. every 3-6 months)
  4. App pulls synced data, including topics list, from Google account
  5. App registers device for push notifications
  6. App subscribes user to "all" topic (all users that want to receive generic notifications are registered here. Unsubscribing is only possible if the user unchecks the "send me notifications" option in settings)
  7. App subscribes to the topics synced in step 4

What isn't clear is WHERE to place steps 6 and 7. Do I put it into my implementation of FirebaseInstanceIdService, into OnTokenRefresh on Android, and in Messaging.SharedInstance.Connect or InstanceId.Notifications.ObserveTokenRefresh on iOS? Do I need to re-register to topics when an FCM token change happens?

The app is written in Xamarin, so we're using FCM for both iOS and Android.

fonix232
  • 2,132
  • 6
  • 39
  • 69

2 Answers2

3

Where you place the code for subscribing the token depends entirely up to you. Usually though, it is placed on the initial activity of the app. This ensures that the user will be subscribed to that certain topic.

However in your use-case, you could make a method that checks if the Notification Settings for your app is ticked or not, if yes, subscribe the token, if not, unsubscribe. Then simply call this method on your initial activity.

With regard to "Do I need to re-register to topics when an FCM token change happens?", you don't have to. Referring to this answer by @DiegoGiorgini:

If the token is "refreshed" the topic subscriptions are maintained.

Community
  • 1
  • 1
AL.
  • 36,815
  • 10
  • 142
  • 281
0

The register to topic should be placed in the View (Activity class in Android). It depends on where you need it. It could be achieved by using this line of code

FirebaseMessaging.Instance.SubscribeToTopic("promotion");

Note that when you subscribes to topic that does not exist, the topic will be created in the server. However the creation requires long time so the topic could not appear instantly in your Firebase console.

Later on, when you want to unsubscribe (probably after logout), just call this inside your View as well.

FirebaseMessaging.Instance.UnsubscribeFromTopic("promotion");

Hope this could help.

Darren Christopher
  • 3,893
  • 4
  • 20
  • 37
  • So what you are basically saying is that I need to subscribe every time the app starts? I was thinking about putting the subscription to the topics into the callback of the token refresh, but I'm not sure if FCM keeps the subscribed topics between token changes. – fonix232 Apr 21 '17 at 10:56
  • It is up to you really if you do not want to subscribe every app start it is fine as well (probably using button to subscribe/ unsubscribe). Just put that code wherever you want on the activity. Token changes is different, it is using service and the service will run whenever you want (for token refresh it should be on app start) so the token could be updated whenever needed. For more information, please refer to the xamarin developer link regarding fcm: https://developer.xamarin.com/guides/android/application_fundamentals/notifications/remote-notifications-with-fcm/ – Darren Christopher Apr 23 '17 at 04:51
  • So if I'm understanding it right, topic subscription is tied to InstanceID of the Firebase client, and is preserved between token changes. – fonix232 Apr 23 '17 at 10:31
  • So token changed and topic subscription are basically different things, however, in order to maintain the subscription you need to refresh the token – Darren Christopher Apr 24 '17 at 06:10