I'm implementing notifications with Firebase Cloud Messaging (FCM) using NodeJs on the server of the company where I work using "firebase-admin" and @react-native-firebase/messaging at Expo.
I'm trying to understand all the limitations of the free FCM before I start coding all the notification management logic, but I don't understand this: "An app instance cannot subscribe to more than 2,000 topics." This is described here in the documentation https://firebase.google.com/docs/cloud-messaging/android/topic-messaging?hl=en-419
This means I can subscribe to a single-user device token:
Expo code:
// https://docs.expo.dev/versions/latest/sdk/notifications/#getdevicepushtokenasync
const token = (await Notifications.getDevicePushTokenAsync({projectId})).data;
Only 2000 topics, but can I create infinite topics? (Note: the subscribeToTopic function creates a new topic if the topic 'name_topic' doesn't exist)
admin.messaging().subscribeToTopic([],'name_topic')
And if I use the following function to unsubscribe with admin.messaging().unsubscribeFromTopic('', 'name_topic')
, will I be able to subscribe the user's device token to new topics?
Can I subscribe a user's device token to any number of topics without limit if I take care to call admin.messaging().unsubscribeFromTopic to remove it from the topic so I never exceed the 2000 limit?
The limitation means that a single device (strictly speaking: a single FCM token) can be subscribed to no more than 2,000 topics at any point in time.
If you've reached the 2,000 topic limit on a device, unsubscribing from a topic will allow you to subscribe to a topic again.
There is no documented limit on the number of topics, so while a single device can "only" subscribe to 2,000 topics you can have (many) more topics across all devices that use your app.
Your entire use-case of subscribing and unsubscribing in a loop sounds very much like a XY problem. In normal use, devices are seldom subscribed to more than a few topics and in my decade of working on Firebase I doubt I've seen more than a handful of projects reaching the 2,000 topics-per-device limit.
Given that topics without subscribers are cleaned up automatically by Firebase, What are you trying to accomplish by creating thousands of topics?