Firebase: Send push notification to user on all their devices - java

I am building an app similar to Instagram. User uploads a post and other others can like it. When people like the post, I want the post author to get a push notification using FCM (Firebase Cloud Messaging).
My problem is that the refreshToken (that is generated using FirebaseInstanceId.getInstance().getToken();) is specific to the device. So if the same user logs into the app from a different device, I want to make sure I send a push notification to all the devices. So here is my proposed solution, I want to know if it's the right way to do it:
Every time user logs into a device, I store the refreshToken into the database and when I want to generate a push notification, I can iterate over all those refreshTokens and send a push notification to all those devices.
Is there a better way to accomplish this?
Note: I am aware of Device Group Messaging and I can't use that because I don't have an app server. And I don't want to do it on the client side because that requires the client to have a Google account.

Yes, the implementation is okay.
Every time user logs into a device, I store the refreshToken into the
database.
This is actually not necessary to send the device push registration id (refreshToken in your case) each time user logs into your application from a device. You need to send the push registration id (i.e. refreshToken) from your device only when the device is not known to the server yet (i.e. new device).
Is there a better way to accomplish this?
I think no. This is the perfect way that you have implemented. However, we might always think of several optimization points like, tracking which devices are logged off, so that you do not need to send the push notification to that device any more. So in case of logging off from a device, send another invalidate refreshToken request to the server from that device, so that it does not get any further push notification. This is an improvement over the current implementation.

Related

How to detect if user cleared my app data

When user clear app data it automatically logout but he still get firebase cloud messaging i want to stop sending notifications after user clear app data .
Firebase Cloud Messaging has no concept of users and/or their authentication state, so if your app behavior depends on those it is because you have made a mapping.
For example, it is common to associate the FCM device tokens with the UID of Firebase Authentication, and then look up the FCM device tokens when you want to send a message to that user. If that is indeed what your application does, you'll need to clear that association when the user signs out to stop sending of messages through FCM.
For example, you could check if a specific, known value still exists in the app data every time that your app becomes active, and clear the FCM mapping if that is not the case. Also see:
How to check if clear cache was done by user in android?
Android: How to get notified if the user clears application data from Settings -> Applications Manager
How do we know when our application data get cleared in android?

How to query message send record in Firebase Cloud Message? [duplicate]

I have an iOS app that integrated with Firebase Cloud Messaging to send push notifications from. I know that Firebase saves history of all sent notifications.
Is there a way for each specific device running the app to fetch its own history of sent notifications? I need to do it on client side, not on server.
There is currently no API to retrieve details of all the sent notifications with FCM (see this and this).
The idea is still feasible, however, you'll have to store the logs/history on your own, in your App Server. From there, you could setup your app to retrieve the details that only correspond to the device.

how to make real time notification in android using retrofit 2

I want to make real-time notification in my android app, but I don't know how to make it real-time and connected to the database
how to make my app can get/access data change from the database every time the database changing even when we didn't open the app
let's say every time admin sending notification, it will send data to database and it has column "read" and it will set to 0 and then user's app will immediately receive notification where the column "read" is 0, and when user opens the notification it will open an activity and change column "read" from database to 1.
You can use either firebase cloud messaging or user background notification service with web socket connection
There are many ways to achieve this type of real-time communication like,
Socket.
MQTT Protocol.
WebRTC.
Firebase Live Database.
Also, you can achieve this feature by sending notification from your admin panel to the user's mobile devices.
A common way is to implement Firebase(FCM) in your application and on your server. You can see how to implement FCM using this Offical documentation help Link https://firebase.google.com/docs/cloud-messaging/

Multiple GCM Registration IDs for the same user sending push notifications multiple times

In the backend I receive GCM Registration_id, user_id (unique), latitude, longitude, score etc from Android application and save in the Amazon DynamoDB table. When I want to send push notifications to all the mobiles, I fetch all the GCM registration IDs, put into a java.util.set to avoid duplicates and then send Push Notifications to all those android devices based on set of registration IDs.
But now the problem is, Since whenever user uninstall and install the android application again, new registration_id will be generated and I receive it & save in database. Hence, same user receive push notifications multiple number of times, and it's because of multiple GCM Registration_ids saved for same user.
How do I solve this problem in the backend? I want to send only one push notification for every user. I am using Java Spring framework? Any example code and code to query DynamoDB will be highly helpful. TIA
You need to keep one entry of GCM Registration_id against each user in the database, whenever a user loagin or Install an application at that time you should send GCM Registration_id to the server and replace the older one with new id... check the following link
http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.Modifying.html#Expressions.Modifying.UpdateExpressions

Push notification in local web without internet

I'm aware of GCM services for push notifications, but I have this issue.
I have a android app which will send a data to a local web server (php) that will response to another android device with the data sent, a normal push notification with GCM, I think.
But I need this to work even without internet, because it is a local web app that will work only that.
Is that even possible? Android device X send json data to web server that will send the data to other android device Y.
How can I verify that exists new data in the device Y ?
Thanks. I know its a little wierd.
Yes, it is possible. But it is not easy to develop. This is why:
Push notifications work attached to an account service. So you would have to implement an Authenticator service and then the whole push platform yourself.
I would not use GCM for what you are doing though. If it is working on a local network, you can use "polling" (request the server every now and then for updates)

Categories