Firebase Cloud Functions - Send Notification user when they are online again? - firebase

I'm using fcm to send user notification and its triggedred by writing data to firebase database. But when user is offline notification send but never received and user cannot see it after they back online. How to send notification in all conditions.
Example: If user online send message normally but if user offline wait until user online and send them. How can I do this?

According to documents firebase cloud messaging already support what i want.
If the device is not connected to FCM, the message is stored until a
connection is established (again respecting the collapse key rules).
When a connection is established, FCM delivers all pending messages to
the device. If the device never gets connected again (for instance, if
it was factory reset), the message eventually times out and is
discarded from FCM storage. The default timeout is four weeks, unless
the time_to_live flag is set.
So FCM service wait until client device connected again.And sen notification when cliend connected. But if user hasnt connected for four weeks message deletes itself and never sent.
https://firebase.google.com/docs/cloud-messaging/concept-options#ttl

Related

Firebase messaging subscribe to topics after token refresh and multiple devices sync

From my testing and understanding,
I noticed that when a user subscribes to a topic using Device A, and logs in on Device B.
Device B will not receive that topic's notification unless the user subscribes to that topic from Device B as well. How can i have both devices receive the notification when only one device subscribes?
When using the FirebaseMessaging.instance.onTokenRefresh.listen() stream, and the users token changes, how can i subscribe his new token to his previously subscribed topics? (Note: I store them in the database).

Can my phone receive Firebase push notification if I have installed an app that support it but I have never open it before

Do I need to open the app at least once in order for my phone to receive a push notification?
The must run at least once so it can collect a device ID token and send that to the backend that will send the message. Without that token collected, the app can never receive a message on that device.
Read more about it: What is FCM token in Firebase?
Actually you do not need the device token to send/receive the messages.
However, it is required that the user give permission to receive notifications, and that would only happen during runtime. I tested this and can confirm it works. The device token is only used to subscribe users to specific topics, but you can send a message to all users that have your app installed without needing to specify device tokens.

make push notification service with signalr

Hi can I make push notification service with signalR?
for example when user start the app,app can recive message and if app closed again app can recive messgae from servers or clients
Until you can send the message to clients that user is online.
And with the closure of the app, Disconnect Communication server with the client. And the ability to send any message disappears.
If you want to send notification after app closed, It is better to use the google service. (e.g: Firebase Cloud Messaging (FCM) services),
Otherwise you can only use signalR for send message in your app.
I hope this is useful description.
If you want the ability to send messages or have messages available to any client then store them in a database table. When they are connected, you can send those messages to them. When they are disconnected they would stay in the database.
You can control flagging messages as read after they are delivered and then deleting them from the database.
Your "push notification" is just querying for the messages when a client is connected.

Getting only last notification on iOS via firebase if not connected to internet

The issue is that I want to send push notifications to iOS and I am able to do it via the token to the specific device and everything is working fine but the issue I am facing is that I only get one one notification if my phone is not connected to internet, when I connect it back to the internet.
Like If I send 5 different notifications to a token via POST Request using Postman and I had turned off my Mobile phone Data and my Wifi and after sometime I turn on any one of them then I only receive the last notification which I request out of the 5.
You should read about 'non-collapsible' messages in the Firebase documentation if you expect to get all messages.
https://firebase.google.com/docs/cloud-messaging/concept-options?authuser=1 clearly states
Except for notification messages, all messages are non-collapsible by default.
Maybe you're sending collapsible messages. And the FCM documentation states that:
If the device is connected but in Doze, a low priority message is
stored by FCM until the device is out of Doze. And that's where the
collapse_key flag plays a role: if there is already a message with the
same collapse key (and registration token) stored and waiting for
delivery, the old message is discarded and the new message takes its
place (that is, the old message is collapsed by the new one). However,
if the collapse key is not set, both the new and old messages are
stored for future delivery.
You can try sending non-collapsible messages.

Firebase Cloud Messaging to send notification for set of users

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).

Resources