Batching up notifications - push-notification

I have a simple chat messaging system where users can send one-on-one messages to each other. It uses SignalR to send chat messages, and then the messages are stored in a database in this format:
SenderId | RecipientId | Message | SentDateTime | RecipientHasRead
Currently, when a userA sends a message to userB, the SignalR service immediately sends a push notification to userB at the same time as saving the message to the datbase.
UserB can then get overloaded with lots of notification. I need some way to batch the notifications.
For example, currently User B will get the following notifications:
10:00am: User A has sent you a new message
10:01am: User A has sent you a new message
10:01am: User A has sent you a new message
10:01am: User C has sent you a new message
I would prefer that User B will get something like this:
10:01am: You have 4 unread messages
What are some techniques to implement this? Should I just run a job every 5 minutes to check un-read messages for a given user in the past 5 mins and send that out?

Related

SignalR push notification when users are not online

I need to implement push notifications for my asp.net core project.For a news service
it needs to work in such a manner that if the user is not online while a message is being sent out, then the message will arrive at the client when they are online thenext time.
All the articles I can find about this, are all slightly vague about this case, and just emphasises the "real-time" communication.
So would it be possible to have push notifications that could that wiht signalr?
it needs to work in such a manner that if the user is not online while a message is being sent out, then the message will arrive at the client when they are online thenext time.
To achieve your above requirement, you can try:
mapping user to the connection id(s) and persist that mapping, which would help find/get new connection id(s) based on user name or email etc readable info after user connect/reconnect to hub, then you can send unread message/data to specific user by specifying connection id(s).
Note: you can also send a message to a specific user by passing the user identifier to the User function in a hub method, please refer to this doc: https://learn.microsoft.com/en-us/aspnet/core/signalr/groups?view=aspnetcore-5.0#users-in-signalr
for unread messages, as #SamiKuhmonen mentioned, you can store/persist those messages in somewhere, and if you want to persist information after restarts or you would host hub on multi-servers/instances, in-memory store is not a good approach. You can try to store messages in database etc permanent, external storage.
define and use a flag to indicates if the message has been seen by user. After client received a message, then invoke a hub method to update that flag or delete that message from the store.

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

how to send data message to user segment From Rest API

I'm using Advanced REST API to send data message for push notification.
In firebase console i can select user segment
as "Version","language",Country" etc..But how to select user segment for
Data message sent from REST API?
Or is their any other way to use select User segment for data message?
Also i want to send the notification for the user who has not opened app for few days.Is it possible with firebase data message?
When using the FCM rest API to send message, you can't target Analytics user segments. You can only send to devices identified by their unique token, or to topics where the client app is subscribed. The only way to send a message to a user segment is through the Firebase console.

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

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

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