How to send a topic message via Firebase ?
I only found the way to send by using Firebase Console ,
as well as some ways about Post HTTP requests ,
but I don't understand how to do it in Android.
How can I write some codes in Android
to specify a topic which to target then send a message to the topic?
Thanks.
Sending a message to devices (so-called downstream messages) requires a HTTP call that specifies the server key. As its name implies, this key should only be used in environments you can trust. You cannot directly send a message from a device to other devices, including topics. This diagram from the Firebase Cloud Messaging documentation shows the flow:
So if you want to send messages from an Android app, you will have to:
create server-side code that the Android app talks to
have that server-side code call Firebase Cloud Messaging to send messages
have the Android app call your server-side code
One way to accomplish such a flow is described in our blog post Sending notifications between Android devices with Firebase Database and Cloud Messaging. It uses the Firebase Database to communicate with a server-side script that then calls FCM to send the messages to topics. The server-side code in this post is a Node.js script, since it was the simplest approach available when I wrote it.
But last week Firebase released Cloud Functions for Firebase. This allows you to run server-side code without managing your own infrastructure, which makes it a perfect fit for your use-case. In fact it is such a good fit that it's first in the documentation on use-cases for Cloud Functions for Firebase:
You will see that the approach in this sample is very similar to the one in the blog post: both listen for database writes to trigger sending FCM messages. Some of the changes in the sample compared to the blog post:
the sample replaces the Node.js script with a Cloud Function.
the sample sends to device tokens instead of topics.
the sample uses the new Firebase Admin SDK to send messages, instead of calling the FCM HTTP end-point
If your app is registered in Firebase console, you may send message not only via Firebase console but via either http client.
Here is code in Fiddler sending notification to "news" topic
Url:https://gcm-http.googleapis.com/gcm/send
Headers:
Content-Type: application/json
Authorization: key=[YOUR_APP_API_KEY]
Body:
{
"to": "/topics/news",
"notification": {
"body": "Hello dude!",
}
}
or use android app to send message
sample from github
Related
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!
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.
I am having trouble understanding the specific meaning of "app server implemented in my own environment" as used in this documentation on how to send upstream messages to the "Cloud" using Firebase Cloud Messaging.
Context
The documentation is saying that for me to send upstream messages, I need my own app server that implements one of two connection server protocols in HTTP or XMPP.
The Reason why I am confused
My expectation is that if I use Firebase, I don't need to create my own server. All of the backend stuff is handled by them. So to me, when they say I need to create my own app server in my own environment, it is contrary to my expectation and understanding and thus makes me second guess the meaning.
What it is I am specifically confused and asking about
What exactly do they mean by app server?
What exactly do they mean by "in my own environment"?
Another way my two questions could be asked is:
Is an app server in this context meaning just a typical app server that I write on my own using something like Node.JS/Express.JS and host on something like Digital Ocean? and/or
Is it something I need to do with other Firebase/Google Cloud products (eg Hosting or App Engine)?
Or could my implementation of the RealTime Database feature on my app be considered an "App Server" as it serves my app with data.
What I have done so far
Reading through every single documentation on Firebase relating to Cloud Messaging and browsing around Google.
I have had a solid read of what questions to ask and what to avoid. I am mindful that this question could be something with lots of varying and all correct answers so apologies if that offends one of the rules. But I have come across this question on Meta which suggests that asking for clarification on documentation is OK for SO.
Is an app server in this context meaning just a typical app server that I write on my own using something like Node.JS/Express.JS and host on something like Digital Ocean?
Exactly.
Is it something I need to do with other Firebase/Google Cloud products (eg Hosting or App Engine)?
Depends on your use-case. But for Firebase Cloud Messaging, when all you need is to send Downstream Messages, you don't need an App Server. You can just make use of the Firebase Console. If you need to send Upstream Messages, then you have to implement your own App Server.
Or could my implementation of the RealTime Database feature on my app be considered an "App Server" as it serves my app with data?
Not exactly. The Firebase Realtime Database stores the data you need, but the App Server needed for FCM is something that can process requests (Send (Downstream and Receive (Upstream)).
App notifications are sent by Firebase Cloud Messaging server. To send these notifications you will have to tell to which device notifications have to be sent.
So you will have to send the FCM Client ID from your backend server (Node.js, PHP server). You will get the FCM client ID when you integrate Firebase into your app. You will have to store the FCM client ID in your backend server like during registration of a user.
To send a notification to a device retrieve the FCM Client id from your backend server and send it to the Firebase cloud messaging server and it will check if the client id is valid and trigger a notification.
I need some clarifications.
I'm using Firebase realtime database.
Now I've to implement a realtime chat between two android devices. I need also push notifications.
So, the solution based on realtime database is to drop because if the app isn't running, it will not handle the onChildAdded event.
I read on the documentation that I can achieve my aim, using the upstream message from the device to the server.
Now, it's written also that we need an app server.
I just want to be clarified the need of the app server (XMPP or HTTP), and which is the entire flow of message m1 sent from the device A, and the notification on the device B.
In my opinion the app server should act as a man in the middle, so:
A ---> XMPP SERVER ---> FCM
FCM ---> XMPP SERVER ---> B
Is this the flow?
On the documentation I can't understand if I'm right or not.
If so, how do I send a message from the FCM back to the XMPP Server?
The notification message is sent from FCM to the client o from XMPP Server?
Please someone clarify me.
Other solution thought by me:
Since I find really powerful firebase realtime database, I thought these solution to walk around the problem:
Create an app server the is listening for onChildAdded and when it changes send a notification to client device.
Creating a background service on the client app listening to onChildAdded and create notification when necessary.
Even all, I don't like these solutions.
Just want to understand the standard and correct way to achieve my aim.
Firebaser and author of the article debated in the question comments here
To send messages to a device with Firebase Cloud Messaging, you need to specify your project's FCM Server Key. As its name suggests, this key should only be used in code that runs in a trusted environment: i.e. a server that you control.
To send device-to-device messages (such as in a 1:1 chat application where the receiving user is not necessarily online) you need two steps:
one user send an upstream message
the other user receives the downstream message
Using Firebase Cloud Messaging and your custom app server code, you can handle step 2. But that still leaves step 1: the user needs to send a message that somehow triggers your code on the app server. There are quite a few ways to do this.
For example you could implement an HTTP endpoint on your app server and have the chat application send the messages there too (in addition to sending to the database).
You could also implement an XMPP endpoint and have the chat application send the message there (again in addition to sending it to the database).
My article suggest yet another way, one that doesn't require implementing an endpoint at all. It uses a node.js script that runs on your app server and is essentially just a client to the database. Whenever this script detects a relevant chat message, it calls the FCM API and sends the downstream message.
This pattern of using the Firebase Database as your endpoint and then using server-side scripts is quite common when using Firebase. We documented it in our classic blog post Where does Firebase fit in your app? (as option 2) and in this article in the Google Cloud documentation.
I'm developing a new chat application that currently works with firebase realtime database and cordova.
I was looking for a backend-less solution since my currently working app doesn't need any server at all apart from a tiny server that its only function is to provide with temporal authorization tokens for the clients.
This tokens allow the client to work directly with firebase without the need for a more expensive and loaded server, and still have a central control for the usage of the app.
By reading the new firebase documentation I believe that the notifications and the firebase cloud messages app can't be used by the client side to post messages, only to listen notifications since all the send message examples expose the server API key, which obviously can't be on the client side.
Is there a way to issue temporal tokens from a central server that can be used by the clients to send messages instead of having to send all the messages to the server and then back to the other devices?
Thanks
Sending downstream messages to devices with Firebase Cloud Messaging requires access to the authorization key. For that reason it should run in a trusted process, such as on hardware you control.
Cloud Functions for Firebase was launched today, which would solve your problem! You can initialize the firebase-admin SDK within your functions code (which runs on Google's servers, not client side), and use it to access FCM. That way you can send messages in response to new database items, or in response to HTTPS requests.
Here's an intro to Cloud Functions for Firebase: https://firebase.google.com/docs/functions/
Here's how you can use firebase-admin to send FCM messages:
https://firebase.google.com/docs/cloud-messaging/admin/send-messages