Maximum number of topics a device can subscribe to in FCM - firebase

Documentation for topic messages for FCM says the following two points
Just wanted to know what is this excessive number of topics? Any approximate value to it? Also, the documentation doesn't say anything about creation of topics. How to create/delete a topic?
Another question -
Document says it takes time to show the topic on firebase console, so, we can't send notifications unless the topic is visible on firebase console (even programmatically)?
Thanks in advance

For future questions, please limit yourself to a single question per post. But here are the answers that I know (or a fairly educated guess):
The limit on the number of topics a client can subscribe to is to counter abuse. It is not a documented value.
A topic is auto-created when a client subscribes to it.
There is no API to delete a topic. It will automatically disappears when nobody is subscribed to it anymore.
You can only send notifications from the console once the topic shows up. You can send notifications from your app server using the Firebase Cloud Messaging API at any moment.

Related

Flutter FCM Topic named like a document

I am new to firebase cloud messaging and i search the best way to send notifications to clients.
I want people to be able to subscribe to new entry in subcollection like this :
books/{bookID}/comments/{commentId}/reply/{replyId}}
Is that bad if i use that kind of syntax?
so i can push notification on that topic when new reply are created
void fcmSubscribe(String bookId,String commentId) {
firebaseMessaging.subscribeToTopic('book-${bookiD}_comment-${commentId}');
}
or i need to use Individual Device Notifications and create entries like this
books/{bookID}/comments/{commentId}/notifications/{tokenId}}
i want to avoid firestore Read and Write.
You can use whatever valid topic names that you want. Use whatever you like - it's your choice. There is nothing particularly "bad" about your choice of name, as long as it works for you. Things can only go badly for you if you exceed one of the documented limits for topic messaging:
Topic messaging supports unlimited subscriptions for each topic. However,
FCM enforces limits in these areas:
One app instance can be subscribed to no more than 2000 topics.
If you are using batch import to subscribe app instances, each request is limited to 1000 app instances.
The frequency of new subscriptions is rate-limited per project. If you send too many subscription requests in a short period of time, FCM
servers will respond with a 429 RESOURCE_EXHAUSTED ("quota exceeded")
response. Retry with exponential backoff.
If you're thinking that FCM is tied in any way to Firestore, that's not the case. You are not obliged to make anything match between your Firesore documents and your FCM topics.

How to use FCM topics for notification in chat app?

I am make chat feature with Flutter and Firestore backend.
Every message is new document in Firestore collection with UID and text field. Chat is 1:1 and random so no know who user will talk to before enter chat. DocID in chat collection are all auto-id.
I have read can use topics to manage send notification. This should be easier than use individual device fcm token.
Anyone know how to implement use topic for this random 1:1 chat app?
You can definitely use a separate topic for each 1:1 conversation, for example with the naming scheme I described here: Best way to manage Chat channels in Firebase. But there are some things to consider which, as Doug already pointed out in his comment, leads most developers to not solely use FCM for their chat apps.
For example: FCM topics are not secured. This means that anyone who finds out the topic ID can subscribe to it, and thus overhear the 1:1 conversation. And while you can generate topics that are hard to guess, you should not rely on not knowing the topic ID as a security mechanism.
Another reason to consider alternatives is that FCM messages are transient: once they are delivered there is no longer any trace of them. With your current Firestore implementation you can query the database to get all messages to show, while with a pure FCM implementation you will have to build your own database (if that is required for your app).
For these reasons most chat apps I know of use a combination of FCM (for push notifications) and an online database (for persistence) as their backend services.
I found Frank's comment really interesting about the "sorted userID composed key". I would probably use that as the chat key in the database (realtime/firestore), however, for the notifications I think I would still use a topic for each user - this way I would be able to avoid notifying the user who posted the message. If that wouldn't be an issue, then just go for just one topic per chatroom.
Also mentioning Frank, I would probably use extra keys in all topic names to make them really difficult to guess. (but add that later so you don't get distracted with non core stuff)
In this answer you have an example of how to post the notifications with a onCreate trigger to a topic (from a functions backend).
In the flutter code, you can use subscribeToTopic from the firebase_messaging plugin to start listening to a topic.
Note: if your app will support user log-off [probably it will :)], then you'll also have to delete the token in the device to avoid receiving notifications from the last logged user.

How many topics an app instance can subscribe in FCM?

As mentioned in FCM documents, unlimited topic can be created for one Firebase application. But as the Firebase Admin SDK document: explains an error:
messaging/too-many-topics:- A registration token has been subscribed to the maximum number of topics and cannot be subscribed to any more.
I was not able to find this threshold value of maximum number of topics. Can anybody explain, what is the limit? How many topics an app instance can subscribe to in FCM?
I found the answer by running a subscription script for an app instance. After subscribing to 1999 topics, for the next subscription it started giving error: messaging/too-many-topics.
So the threshold value is 1999.
As much as the selected answer is correct, I'll like to post an absolute answer here.
Google Firebase Cloud Messaging
Some things to keep in mind about topics:
Topic messaging supports unlimited subscriptions for each topic.
However, FCM enforces limits in these areas:
One app instance can be subscribed to no more than 2000 topics.
If you are using batch import to subscribe app instances, each request is limited to 1000 app instances.
The frequency of new subscriptions is rate-limited per project. If you send too many subscription requests in a short period of time, FCM servers will respond with a 429 RESOURCE_EXHAUSTED ("quota exceeded") response. Retry with exponential backoff.

Does Ionic 2 support private & public Firebase notifications?

I'm thinking to work on firebase notifications in the last few days but I want to some answers for this few questions:
Does Ionic2 support firebase notifications ?
Will notification show in the top of screen when my application closed or it should be work in the background ?
Is firebase notification sent on time or it may take long time ?
If I assumed Ionic 2 can send private notifications (to my device only), so can I send public notification for all application users ?
If the answer of all questions "yes", wish you give me any useful links that may help me (if you have a time)
In addition to Gabriel's answer:
The default behavior for notifications is for it to show on the top part of the screen. I haven't worked on Ionic before, but AFAIK, the behavior should compared to Android.
With that said, you should refer to the Handling Messages part of the FCM docs.
There are factors to consider that may affect the time it takes for the device to receive a message, but as FCM's behavior, it would try it's best to send the message, as soon as feasible (see my answer here).
Gonna itemize this further:
Specific device only - Yup. When sending the message, just specify the registration token(s) that should recieve that message.
All devices - Yup. You could use the Firebase Notifications Console. But if you intend to send the message by using the REST API, you have to make sure that the devices are subscribed accordingly to your custom global topic.
About the device-to-device Gabriel mentioned, I think it should be worded as server to specific device(s).
Yes it does, and it works fine with FCM (Firebase Cloud Messaging, aka GCM - Google Cloud Messaging). It supports subscribing to topics and device-to-device notification via HTTP client. The topics subscriptions and device-to-device both require FCM plugin.
Yes, they'll appear on top of your device screen as any push notification, you'll just need to code how it'll behave after receiving it, like execute something if the notification is tapped, or do something if it's received and the app is already open (foreground), it's all up to you.
It's sent on time, i've never had delay problems, the longer it took to send the notification and i receive in my device was 10 seconds. But as far as i know it "stores" your notification for 24 hours, so if the servers go down or something happen the notification has a life time of 24 hours to be sent.
You can, as said in first answer, subscribe your users to topics. Let's say you want a topic to sent message so all users can receive, you can have an all topic, or have one only for logged users, one only for users who bought products, you can even get all users tokens and send one by one, but it's much better having them subscribe to topics.
Topics notifications can be sent via the Firebase project console. Device-to-device is sent by your app code.
To send device-to-device you'll need a token, it's provided by the FCM plugin (link above) and you'll need to store this token somewhere, like in your user node on Firebase. I usualy use the user device ID provided by the Device plugin to have his token, since the user can have multiple devices and log in all then, this'll prevent token overriding.
I don't know good tutorials about configuring FCM with Firebase, if i find something i'll edit the question and add it for you, but stick to the ionic native link and the FCM plugin github page.
Hope this helps.

Does Firebase FCM support retrieving old messages?

Does Firebase Cloud Messaging provide any way for a user to retrieve old messages? From the documentation, it looks like messages are thrown away once the client receives them.
I ask because I'm looking to build an IM functionality in my app, and a user needs to be able to see past messages when he closes the app and then opens it again.
Update from the comments here:
Unfortunately this just got worse as of 20 Nov 2017 when FCM Diagnostics was removed from Google Play Console. "I understand that FCM Diagnostics was critical to you in troubleshooting FCM messages but unfortunately this has been deprecated. Rest assured that we are working on something better that should allow a lot more insight into what went wrong during message delivery, but we can’t share any timelines. I’ll share your concern internally and continue to work to get this new feature out.
FCM's main purpose is for Push Notifications. So to answer directly, No. FCM doesn't keep track of the message you send for you. There is a diagnostics and statistics tool you can use, but I don't think this is the one you're looking for.
IMHO, it's the developer's responsibility to keep track of the details they need. In your scenario, you would be needing a database to store the message details themselves, wherein you can make use of Firebase Realtime Database. There's actually a sample Codelab about creating a simple chat app using Firebase here.

Resources