Push Notification to Specific Logged In User using Firebase/Batch - android

I'm building an app that allows the user to create events and add people to those events. It stores the event and certain user data (email and display name) in a Firebase database.
What I'm trying to do is send a notification to a specific user when another user adds them to an event. I've tried looking at the documentation for both Firebase Notifications/Cloud Messaging and Batch, but I can't find a way to send the notification to a single user, rather than an entire group.
How do I go about doing this? I'm hoping to avoid storing the notification in the Firebase DB and setting up a servlet backend that queries the DB repeatedly.
Edit: If I do have to make a Java Servlet backend for this, can someone tell me how I would code it?

I think you can do that by specifying the device token when sending the notification using console or using post request to FCM server. But unfortunately you have to handle every single device token manually by yourself (get it from the device when it register to FCM, and save it to your own DB somewhere).
If you use console, you can put the token on "FCM registration token" field under target -> single device.
If you use post request, you can make a request like this
{
"data": {"my_custom_key" : "my_custom_value"},
"registration_ids": ["your-device-token","your-device2-token"]
}

Batch.com provides a simpler API named Transactional API to send a notification to one or more users identified by their User ID, Firebase UID etc. as you probably don't want to deal with the complexity of APNS push tokens or GCM registration ids yourself. Contrary to FCM, you will get a complete analytics view (eg: open-rate) on those notifications.
You can trigger push notifications directly from your app code by using our Swift client for the Transactional API or by coding your own HTTP client (it's a standard POST request with a JSON body). We even have a client for Node.js if you decide to trigger the notifications from a server.
And if you are using Firebase, we have a specific documentation to plug Firebase with Batch.

Related

Sending push notification to a specific user in cloud function FCM

Please note that this question is not about getting device token on client side.
I am very new to react-native and firebase and I want to implement push notication service of FCM. What I am planning to do is to send a notification to particular device using its device token on my nodejs app server or cloud function( not through console). But as I have found, FCM doesn't provides any API to accrss token by username. Suppose I want to send notification to user X( for the time being, suppose that one user signs only on single device). Now using the function sendToDevice(), I can send the message to a specific device. But how would I know the device token of user X. Do I need to store tokens in the firebase database by myself? Or can I get along without storing FCM tokens?
Please guide me because firebase docs aren't clear about this.
Save the device token for a particular user in DB and then you can use fcm-node npm in which there is a simple function to send a push to a particular device token.
You also need to use the FCM server key which you will get easily on the firebase console.
FCM doesn't offer a way to associate device tokens to individual users who might be using your app from multiple devices. You will need to write code to associate a device token to a user account by sending that token to your backend, along with the user's ID. Only then can you collect FCM tokens for that user, and message them on any of their devices.

generate notification with android client app using firebase cloud message

I would like to push some notification with a android app. So i did some researchs, but i'm little lost.
It's possible that I'm wrong but i think my only possibility is to use FCM with HTTP Request like that :
POST https://fcm.googleapis.com/v1/projects/myproject-b5ae1/messages:send HTTP/1.1
Content-Type: application/json
Authorization: Bearer ya29.ElqKBGN2Ri_Uz...HnS_uNreA
{
"message":{
"topic" : "foo-bar",
"notification" : {
"body" : "This is a Firebase Cloud Messaging Topic Message!",
"title" : "FCM Message",
}
}
}
But i see everyone use their own database and i don't know why they need to use it. I would like not to have to use my own database, so if you have a better solution please help me.
Thank you for your all responses
(sorry for my english)
Sending a message to a device with Firebase Cloud Messaging requires that you specify the FCM server key. It is the value in the Authorization: Bearer ya29.ElqKBGN2Ri_Uz...HnS_uNreA header in your sample.
But a key concern here is that anyone with your FCM server key can send messages to all users of your app without restrictions. And if you embed that key into your app, it's only a matter of time before someone discovers is and uses it to send unwanted messages to your users. That's why, as its name implies, the FCM server key should only be used in trusted environments (such as a server/device you control, or Cloud Functions).
So while it is technically possible to send a messages from one Android device to another, doing so exposes users of your app to risks that you should not want.
Some more links to good previous answers and articles:
This answer from Diego one of the engineers who works on FCM.
My blog post describing the above in more details and how to send messages between Android devices securely.
The Firebase documentation example on sending messages securely from Cloud Functions
Some of my many answer on sending device-to-device messages with FCM.
I had this same problem a while back... To cope with sending notifications via FCM without a database, I used Firebase Cloud Functions and the Firebase Admin SDK to do this. I needed to send a notification when the user received a chat from the group or from another person. I used one of the 4 triggers in Firebase Cloud Functions. The one I used was the onUpdate() trigger. There are also onWrite(), onDelete(), and onCreate. All of these triggers make it possible to send a notification whenever there is a such change.. I used the Admin SDK then to get the token(if it was a single user, which I had previously uploaded to the database) or the topic(which for group chats, I named after the group chat so it would be easy to get). From this, I also named the type so it would know whether to send to a single user or a group. This allowed me to have a listener to send notifications without needing a database of my own. All through firebase. And another fact to add is that this is done in Node.JS, but it is fairly easy to grasp the concept with a knowledge of Firebase.
Some useful links:
Getting Started with Cloud Functuons
Realtime Database Triggers
Admin SDK Setup
Admin Database Getting Started, next pages for read and write(remember to use Node.JS)
Hope this helps!

How to send notification from one user to another using firebase?

I am trying to send the notification from one user to another user of the same app.
I am following this article : https://firebase.googleblog.com/2016/08/sending-notifications-between-android.html
Here it takes username and message. But the username is plain name, how it will identify the which device it needs to send the notifications.
Or it is firebase id: FirebaseInstanceId.getInstance().getToken()
Or I have to manage mapping of user to device on the server.
And what is FIREBASE_URL?
The article you read uses FCM topics to identify users. This is a naïve-but-simple way of addressing users. If your needs are more complex, you will need to manage your own mapping of users to tokens on your server.
For an example of using Cloud Functions to send FCM messages to tokens, see this sample in the Firebase documentation.

how to send fcm notifications to a single device without using firebase console

I am storing the registration tokens in my database. I want to send push notifications to the single device by taking registration tokens from database but without using firebase console. How to do it?
You need an app server or some Google Chrome extention like PostMan.
First of all, you need to understand the differences between notification message and a data message, like how the app handles the incoming message if it is in the background state or on running on the foreground.
Secondly, create a payload and send a post request to FCM endpoint. It is well documented here.
Lastly, check the FCM response. Note that getting a success response doesn't mean that the message is already delivered to the app, but rather the FCM server accepted it for delivery.
If you would like to learn more about what parameters you can use and the definition of each response codes, check out the Firebase Cloud Messaging HTTP Protocol documentation.
I hope this helps.
If you are using Firebase database to store your tokens and intend to send the FCM notification during an event, I suggest making use of Cloud Functions for Firebase. See my answer here for an idea on how you might use it.
If you just want to send a simple downstream message, you could use Postman (also stated by #looptheloop88 in his answer) or cURL.

FCM notifications sent to topics not received anymore

My Android client app does not receive any Firebase push notifications targeting topics, however I immediately receive notifications sent to all app users or notifications sent to specific devices.
I didn't change anything in my code and I checked whether the client is correctly subscribed to topics.
For further details about my subscription logic:
In order to make it easy for my web service to send notifications to a specific user, each user is subscribed to a topic entitled with his user-id whenever he logs in from the client app.
Is this approach weak somehow? Should I otherwise register the device token to my database every time it's updated? And then send the notification to that specific token?
Should I otherwise register the device token to my database every time it's updated? And then send the notification to that specific token?
It is highly suggested that developers save the generated registration token for each device for later use. As mentioned in the docs:
After you've obtained the token, you can send it to your app server and store it using your preferred method.
In your case, it is preferable. It'll remove the added action of subscribing the device to a topic. Plus it can be useful to track the message status using Diagnostics tool should you need it in the future.

Categories

Resources