Web Push Notification - push-notification

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 )

Related

Is there a way to send with FCM a push notification directly to a specific device?

I am doing an application in health in Nativescript and using https://github.com/EddyVerbruggen/nativescript-plugin-firebase
where I need to send a specific user a notification to alert him about something.
I have seen that to send to a specific device, the registration token can be used but is there any other way?
PS : when the user log in, I have an internal id that can be used to identify the user.
There is no other way to securely target an individual device. The registration token is the way the FCM identifies the device.
If you would like a non-secure way of delivering the message, you could try to use a uniquely named topic, but be aware that any device attached to your project will be able to listen to every topic if they just know the name.

When and what Old FCM Tokens do I need to delete from my backend?

The way I understand Firebase Cloud Messaging at the moment, I will want to save all FCM tokens a user might have across devices to my backend and send a notification to all of these tokens at once when I want to notify that user about something.
Multiple tokens
Until this point, I assumed that each device only has one active token, however, reading this section of the documentation:
To enable this feature, make sure you have each sender's sender ID. When requesting registration, the client app fetches the token multiple times, each time with a different sender ID in audience field, using the token retrieval method for the given platform
Does this mean that I might need to target multiple active tokens per device?
Deletion
And now coming to the really important question. A simple solution to the above problem would be to simply store every token I ever retrieve in the backend and thus ensuring that my user will always receive the message.
However, what do I do if the user signs out of my app using Firebase Authentication, i.e. a different account is used in the same app on the same device?
I assume that the tokens I sent to my backend for this device will still be active - so now this user will receive notifications from another account because that account was signed in on the device previously.
I do have access to the current FCM token and I could delete that from my backend before signing out the old user, but considering the "Multiple tokens" section: how do I make sure that I can delete all FCM tokens of the old user from my backend?
Uniqueness
Additionally, assuming that old tokens are just dead for the device (will not trigger notifications anymore) when new ones are generated, can I be sure that this token will never be assigned to another device in the future?
TL;DR
How can I make sure that I have the correct FCM token(s) for my user stored in my backend and more importantly: how can I ensure that no tokens of other users are saved for some user in my backend?
I read through:
https://stackoverflow.com/a/40158260/6509751
However, I still do not know how to deal with multiple tokens.
Does this mean that I might need to target multiple active tokens per device?
An application has a single active token for each sender ID. It's fairly uncommon to have multiple sender ID, and you'd usually know if if you do. If you're sending from a single back-end, there's usually no need for having multiple sender IDs.

Firebase functions -get fcm token from user id

I want to send push notification to specific user,
but i dont know his fcm token.
Is it possible to get the fcm token of user from his user id?
A user may be using multiple devices. You should expect this to be the case. You'll need to store each device token for all devices that a particular user may be using in order to notify them. Typically, Firebase apps will use Realtime Database to record the token at the time of login.

Firebase Auth + FCM

My usecase it to send a push-notification to a user using the Firebase Auth UID to all the devices he is signed-in. Since firebase Auth is managing the user sessions,
Is there a way to directly send a FCM notification just using the uid of the user? Or
Is there a way to fetch the registration ids of all signed-in devices for a given uid?
This looks like standard requirement, for example to update the 'Order Status' to a user. If available this could be pretty powerful, and could greatly simplify direct messaging requirements (e-commerce, chat etc) where the user gets notified on web/android/ios.
If this is not possible, any suggestions on the standard way to acquire/manage the registrations_ids of a given user is appreciated.
Thanks all,
This is not supported out of the box but you can build the mechanism.
You need to maintain a uid specific deviceGroup and all the registration ID per device that belongs to it.
Each time a user signs in with Firebase, you get the device registration ID and the user's ID token and send it to your backend. You verify the ID token and get the uid from it, you then add that registration ID to that user's device group.
Each time a user signs out, you remove their registration ID from the signed out user's device group.
When you want to send a push notification to a specified user, you can send it to the user's device group ID.
This is the simplified version but there are a bunch of edge cases you have to deal with to keep the device group for a user in sync so no notification is sent to a signed out user.

Send Firebase Cloud Messaging notification to users by user property

I'm trying to send out an FCM message to a specific set of users (or really, a single user) based on a specific user property but looking through the FCM HTTP API I can't seem to find a way to do that. I can send to users via topics, registration tokens, and device group notification keys, but I don't have any of that infrastructure set up in the near term. I know this functionality exists as you can send such a message via the UI, but I'm not seeing how to do it in the API documentation as yet.
There is currently no parameter that you could use to specify a user property (or even for user segments) that will serve as a target for the FCM API to send the message to.
As you've already searched, the only targets possible are single/multiple registration tokens (to and registration_ids), topics, conditions, and device groups (notification_key).
The option you're looking for is currently only available when using the Notifications Console.

Resources