In Firebase cloud function can send push notification to any user if the user device token is stored in Firebase database. Again if the user subscribes to a topic then also another user can send a notification to the user. Now the question is that in what situation we should use topic messaging and in what situation we should use cloud function to send the notification. If any user subscribes to his unique ID (as provided by Firebase) then anyone can send topic messaging to him by publishing topic messaging to that unique ID. Is it a good approach or we should use cloud function to send push notification to that user using the device token. Is it a good idea to subscribe to his own unique ID to get a notification. Please help me to resolve my issue. Is topic messaging is free to use?
Firebase Cloud Messaging is completely free to use, including the use of topics.
When you use topics, you separate the sending of messages about a topic, from the fact that an install of your app subscribes to that topic. This means you can add subscribers to the topic later, without having to write additional code or even data (as the list of tokens that are subscribed to a topic is handled by FCM itself).
On the other hand: topics are public. Once somebody knows the topic ID, they can subscribe to that topic, and receive any messages you send to that topic.
The alternative to using topics is sending messages directly to FCM Instance ID tokens. In that case you'll keep a list of tokens somewhere yourself, and determine what token(s) to deliver the message to. In this case, you fully control who receives the message, but will have to maintain your own list of tokens, and the mapping of what token receives what message(s).
Note that sending messages (no matter whether to topics or to tokens) can be done from any trusted environment, like your development machine, a server you control, or Cloud Functions. And sending messages (no matter whether to topics or to tokens) can't be (securely) done from the client-side code.
Related
I'm developing an application for my College:
Users (students) login on it with their college credentials, once they are authenticated, the app creates a Firebase Database data for that user containing al his info, including the disciplines(subjects) that he is on course.
Then comes the Firebase Cloud Messaging part, the users can send notifications for other users that are on the same disciplines.
Example: I'm coursing Math, then can I send a notification to the others users that are also doing Math. There are hundreds of disciplines in my College.
My idea is to send the notification to all of my app's users then, before notifying, handle with some code to check if the user is registered to the notification's target discipline or not, if he is, send notification, else, do not send anything, in others words, filter the message before showing the notification!
I studied several Firebase Cloud Messaging docs and examples but I couldn't find a way how to do it... Can somebody give a light?
Yes, actually, it'd be best to decide server-side on the group of users that will receive the notification, meaning, you need to have them in some sort of group. Firebase has a concept of group and subscription for messaging. And you can setup a cloud function to actually send the message to the clients
https://firebase.google.com/docs/cloud-messaging/js/send-multiple
I am working on an app, which requires Android push notifications to be implemented.
I have decided to use Firebase Cloud Messaging directly; without using any other abstraction such as AWS SNS or Pusher.
I would like to avoid storing and managing device tokens in the backend, by using the following approach.
In the android app.
When the user logs into the android application, obtain device token but not send it to the server.
Subscribe to a topic that is based on a agreed convention, such that the topic is unique to that user.
On logout unsubscribe from the topic.
In the Server.
Whenever a situation arises to send a notification to particular user, send push notification to the topic, that is based on the convention.
I would like to know if this is a viable strategy to avoid managing device tokens ?
Case against using topics.
From the official docs.
Based on the publish/subscribe model, FCM topic messaging allows you to send a message to multiple devices that have opted in to a particular topic. You compose topic messages as needed, and FCM handles routing and delivering the message reliably to the right devices.
For example, users of a local weather forecasting app could opt in to a "severe weather alerts" topic and receive notifications of storms threatening specified areas. Users of a sports app could subscribe to automatic updates in live game scores for their favorite teams.
I see that topics are recommended, when multiple devices are to be notified. But I have decided to create a topic per user, this would mean most topics would end up getting subscribed by only one device; Is this approach ok ?
I see that topics are recommended, when multiple devices are to be notified
Yes, multiple devices that have something common to listen to, which is the topic. Topics are used for messages that could be received by the general clients since it is public -- i.e. anyone could subscribe and receive messages sent to it.
What is advised to use for multiple devices, but for the same user is to use Device Groups (see my answer here for tips on Managing Device Groups). However, if you don't mind the "topics being public" part, then your approach should be fine.
Yes, Here required device tokens if we want to send push notification whoever installed your app.
My research we can save device tokens in back end at first time installation of your app that is better according to my understanding so that we can easy to send push notification across all devices.
I am trying to send the notification from one user to another user of the same app.
I am following this article : https://firebase.googleblog.com/2016/08/sending-notifications-between-android.html
Here it takes username and message. But the username is plain name, how it will identify the which device it needs to send the notifications.
Or it is firebase id: FirebaseInstanceId.getInstance().getToken()
Or I have to manage mapping of user to device on the server.
And what is FIREBASE_URL?
The article you read uses FCM topics to identify users. This is a naïve-but-simple way of addressing users. If your needs are more complex, you will need to manage your own mapping of users to tokens on your server.
For an example of using Cloud Functions to send FCM messages to tokens, see this sample in the Firebase documentation.
My Android client app does not receive any Firebase push notifications targeting topics, however I immediately receive notifications sent to all app users or notifications sent to specific devices.
I didn't change anything in my code and I checked whether the client is correctly subscribed to topics.
For further details about my subscription logic:
In order to make it easy for my web service to send notifications to a specific user, each user is subscribed to a topic entitled with his user-id whenever he logs in from the client app.
Is this approach weak somehow? Should I otherwise register the device token to my database every time it's updated? And then send the notification to that specific token?
Should I otherwise register the device token to my database every time it's updated? And then send the notification to that specific token?
It is highly suggested that developers save the generated registration token for each device for later use. As mentioned in the docs:
After you've obtained the token, you can send it to your app server and store it using your preferred method.
In your case, it is preferable. It'll remove the added action of subscribing the device to a topic. Plus it can be useful to track the message status using Diagnostics tool should you need it in the future.
I'm currently implementing Google Cloud Messaging into my app. Basically my app has categories and I need my users to be notifed about recurring events in those categories.
I decided to use GCM Topics as a simple way to notify all the users subscribed to a specific category (I will have about 200-300 topics).
My question is : Since I don't plan on notifying a single user but instead all users subcribed to a specific topic, is there a point in collecting the registration tokens on my server ? Those ids are not required to send a downstream messages to a topic.
I currently have ~20 000 users
Message sent to GCM Http Connection Server
{
"to": "/topics/category1",
"data": {
"message": "Hello subscribers of category1 !",
}
}
Thank you
As it isn't the best practice but technically there is no need to store tokens on your server if you just want to use topic messaging.
You should be aware of these points:
You can't send a push notification to a specific user anymore.
You have to handle onTokenRefresh() carefully and eachtime you get a new token register the user for all of the desired topics agian.
As topic messaging is a new feature on GCM some of the users with old version of Google Play Services maybe unable to subscribe to topics. See this page.
Each topic has a limit for maximum number of registered users (1 Million user per topic)