I'm using FCM FirebaseMessaging to send push notifications to user of my app.
Is there a way to get all notifications sent to one single user or I have to store them in Firestore db?
It's not entirely clear to me what you mean by "get all notifications sent to one single user". But if you're trying to get a history of all messages previously sent to a user via the FCM API, that's not possible. FCM doesn't remember anything about messages previously sent. You have to keep that record yourself, if that's what you need.
Related
We are building an app with chat functionality in Flutter, and I have figured out almost everything about how it should be done, except how the notifications can be sent to only one device (Or possibly a few devices).
The best option so far is to send the notification to an FCM registration token since we use Firebase for all of our back-end, but I can't do that without knowing the token. And the only way I can know that is to store it in Firestore (For example the users document) and retrieve it when needed. Is this a good solution, and what happens if the registration token changes? Should I update the token every time the user opens the app to make sure that it is correct?
You don't need to store the token on the server, you just need to know who you are sending the message to. Just follow these steps
Client
1.a When you app starts, retrieve the token via getToken()
1.b Store it locally
1.c if it has changed, send it to a Cloud function, and register it to a topic or device group
1.d Dont forget to register to onTokenRefresh() to repeat 1.a-c for new tokens
Server
2.a Implement the function corresponding to 1.c
2.b When required, send messages to a topic or device group
In Urban Airship, when I am composing a notification, I can target specific users by searching for a Urban Airship channel_id (device ID):
I sent from Urban Airship the push notification that I am showing above, and I received it successfully. Now I want to do the same thing, send a push notification to a specific device, but now using the Firebase console. The problem is that in Firebase, in the step where I need to specify the target, it only allows me to choose User segment or topic. I was expecting to see a third option: Target specific users (for sending notifications to one or many specific GCM/FCM device token IDs. So my alternative is to add a GCM/FCM device token ID to a topic and then send the push notification to that topic, which is something that I have successfully done before. Nonetheless, that would be a workaround and not the way I would prefer to do this. Is it possible to send push notifications to specific users (by defining the target GCM/FCM device token IDs) from the Firebase console? Thank you.
UPDATE 1: See how the Firebase console (https://console.firebase.google.com/) only shows User segment and Topic as the Target:
First Step:
Second Step:
Third Step:
By design, the notification feature in the console is for sending out broadcast-type events. Sending user-specific alerts would be more of a programmatic operation done through the API.
For sending test messages, there is a console tool for this, explained here.
Note that it is possible to send a notification to one device by subscribing a device to any topic and sending the notification to that topic in the console (keep in mind topics are public and you can't prevent users from signing up to them; fine for testing most likely).
Another alternative is to send a message via HTTP or curl. Perhaps the best resource for this is the quickstart/messaging example.
I'm having some series of questions about implementing push notifications. The things are,
I am using Angular for my frontend where am sending my subscription value which is of an object type with key keys like endpoint, auth, etc (is this will be same for every device like mobile, or some other browsers).
Does all the data in that subscription object is mandatory? or only endpoint.
If a user is logged in with more than 10 devices, do I need to store subscription values for each device? is it how it works? Or should I store the last logged in device's subscription value? If so then the rest of 9 won't get any notifications.
If you are storing all the loggedin Device's subscription value, then is a user logged in more than one browser will he get the notification in each browser? Is it a standard practice?
Suggestions are welcome, any standard practices will be helpful.
Thanks in advance
subscription object has the same format for every web device/browser
yes, for more, see the documentation
When we want to send a push message to a user with a payload, there are three inputs we need:The payload itself.The auth secret from the PushSubscription.The p256dh key from the PushSubscription.We've seen the auth and p256dh values being retrieved from a PushSubscription but for a quick reminder, given a subscription we'd need these values:
each device has it's own endpoint and auth keys, so if you want to send push to every device user logged in, you need to store all of them
yes, user gets notifications for all browsers, if you call them.( you're making a post request to device's endpoint )
I'm developing an app using Firebase, in the app there are groups and for groups there are multiple members. I want to send notifications to members in a particular group based on one of the users activity. As an example in a chat app, notification to others informing there is a new message.
I referred this blog post. As it seems best way to do this is Firebase Topic Messaging, all members can be subscribed to the group id as a topic.
My matter is how to avoid receiving notification to the member who's action causes to the notification.
I tried Firebase message sending to multiple topics but it only allows for two logical operations (&&, ||)
Firebase Cloud Messaging has two types of messages: notification messages and data messages.
The default behavior of Firebase is that notification messages will automatically be displayed if the app is in the background or not running. This behavior cannot be modified. If the app is in the foreground, the notification message will not be automatically shown, it will instead be delivered to your app's code.
Data messages on the other hand will always be delivered to your app's code.
Since the user sending the message will typically have the app open, the default behavior will automatically ensure the notification is not shown on the sending user's device.
But in the blog post you refer to there is a race condition for this. If the user closes the app between the time they send the notification to the server and the time the server sends out the notification, the notification may show on their device too.
If this is a concern for your app, you should not use a notification message. Use a data message instead and include enough information in the message to detect which user has sent it (e.g. the user's uid).
Yesterday Google has announced a new set of tools for Firebase, one of them was Notifications the ability to send notifications from server to devices which are using my app.
But can we now notify users when they receive a new message?
And if not, is there a way around to achieve this?
Looking at the documentation, this doesn't seem currently possible automatically. Here is a possible way to accomplish it "manually" with another server:
Subscribe a user to it's own user ID
In android
FirebaseMessaging.getInstance().subscribeToTopic("InsertUserIDHere");
In IOS
[[FIRMessaging messaging] subscribeToTopic:#"/topics/InsertUserIDHere"];
Setup an outside server that checks every sent messages. When a message is sent, the server should create a notification that includes the recipient's user ID as the topic.
Look here for more info.
You can send messages to group of users (targeting a specific "topic") or to a single device (targeting a Firebase Cloud Messaging token).
To subscribe a device to a topic use:
FirebaseMessaging.getInstance().subscribeToTopic("topicName");
To obtain the device token use (*1) :
FirebaseInstanceId.getInstance().getToken();
Then you can use the Firebase Notificaitons web console, or the FCM server API if you want to send messages from your server.
See: https://firebase.google.com/docs/cloud-messaging/downstream#sending_topic_messages_from_the_server
Notes:
[1] getToken() can return null if the token is not yet available.
You can use the callback onTokenRefresh() to be notified when the token is available and when the token is rotated.
See: https://firebase.google.com/docs/cloud-messaging/android/client#sample-register