1

I am developing an app for which I use firebase as the backend. I am using FCM to send notifications to my users, however, I am not yet grasping how to use subscribeToTopic and unsubscribeFromTopic.

My use case (which I do not know how to get it working): After a user installs the app, he will be subscribed to the main topic of the app (I have this working). The user could then go to settings to unsubscribe from the main topic (I do not have this working).

The struggle: All the different tutorials I find describe how to subscribe to a topic, they call subscribeToTopic('some topic') in initstate of the welcome screen. Is it required to do this everytime the app starts? What happens when the user calls unsubscribeFromTopic('some topic') and the next time they start the app subscribeToTopic('some topic') is called again in initstate?

My idea: first time the app is loaded I call subscribeToTopic('some topic') and never again. Then in the settings screen a user can unsubscribe (and subscribe). Does this work? What should I pay attention to?

Thanks in advance for helping!

BJW
  • 925
  • 5
  • 15

2 Answers2

1
  • You check if the app is launched for the first time by either using shared_preferences which stores this information locally or by storing a variable on Firestore that tells you if the user has logged in to the app before.
  • You subscribe to the topic if it's the first time and if it's not, you do not subscribe.
Victor Eronmosele
  • 7,040
  • 2
  • 10
  • 33
0
  • The first time you open the app show a welcome screen or something like that, where you ask the user to receive notifications or not (which is always more user friendly).
  • If the user continues you save this value in a local database like shared preferences or hive.
  • If the user subscribed you call the subscribe to topic method
  • If the user does not subscribe ofcourse you do nothing.

Then on the settingspage:

  • Show a switch which value is gathered from the local database you already defined.
  • When the user taps the value is stored, based on this value you subscribe or onsubscribe.

This is how I have done it in my app where I also use topics instead of tokens.

Roderik
  • 78
  • 5