I figured how to send and receive Firebase Push notifications using Firebase Cloud Messaging
I want to know how to send a notification manually using code in android studio so I can send it from any phone.
You send messages from your phone using FCM. You need to make a POST to https://fcm.googleapis.com/fcm/send api with payload that you want to send, and you server key found in Firebase Console project.
As an exameple of payload sending to a single user with to param:
{ "data": {
"score": "5x1",
"time": "15:10"
},
"to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1..."
}
Also, to param can be used for topics "to": "topics/yourTopic"
In data you can send whatever you want, message is received in onMessageReceived() service from Firebase.
More details you can found in Firebase documentation.
Related
I have a scenario in my app such that, a certain event occurs and I have a list of user-id/tokens and I need to send the notification to all of those n devices.
To trigger the fcm with n tokens , n time will not be feasible
so I should create a topic dynamically and subscribe those n users's device id/ token to that topic.
I know I can do it from the client app , but is it possible to do that from backend.
I am using Phoenix as my backend.
I found the way, writing this answer in case it help others in future
Yes Its possible to create a topic dynamically if we have the list of
valid registration tokens
This is the endpoint url if you want to generate a topic , given you have a list of users-
https://iid.googleapis.com/iid/v1:batchAdd
The Authorization header contains-
Content-Type- application/json
Authorization- key=<your-server-key>
The body parameters look like-
{
"to": "/topics/<topic name>",
"registration_tokens": [
"token1",
"token2"
]
}
And now the topic is created,
You can easily sen message to that topic with- https://fcm.googleapis.com/fcm/send
Authorization token is same as previous one
And body as-
{
"priority": "HIGH",
"notification": {
"title": "New Text Message",
"image": "https://firebase.google.com/images/social.png",
"body": "Hello how are you?"
},
"to": "/topics/<topic name>"
}
To trigger the fcm with n tokens , n time will not be feasible
Using topics does not inherently change how FCM message delivery works. When you use a topic, the Google servers keep a mapping of that topic to the subscribed tokens. So when you call the API to send a message to a topic, the Google servers fan-out from that topic to the tokens, and then deliver the message with the same infrastructure as when you call the API with the tokens yourself.
Since you already have the tokens, so it might be simpler to just send to them directly, rather than creating a one-off topic.
I am creating a college Android application where faculty choose the specific course and send them assignment notification but how i do that i don't Know... please help me..
You need to subscribe topic from Android app. Send notifications to that topic so it will received to all users who has subscribed it.
Sample code subscribe top from Android app.
FirebaseMessaging.getInstance().subscribeToTopic("news");
Send message to this topic from server-
URL- https://fcm.googleapis.com/fcm/send
Body-
{
"to": "/topics/news",
"data": {
"message": "Message to user",
"title":"title of message",
"sender":"sender name"
}
}
headers- api key from firebase
Rest you need write code from Android and Server side.
You can also send notifications form Firebase console as well.
I tried a sample code to test the push notification system with Firebase and it's working well except one thing.
If I try to send a notification from Firebase console, using Device Token, the notification shows in device.
If I try to send a notification from Firebase console, using my topic topik, all notifications show in all devices.
If I try to send a notification from my web page or from postman, using Device Token, the notification shows in device.
If I try to send a notification from my web page or from postman, using my topic topik, NOTHING HAPPENS.
This is a example call:
link: https://fcm.googleapis.com/fcm/send
POST method
Header field:
Content-Type : application/json
Authorization : key=MY_SERVER_KEY (the new one)
Body:
{
"to": "/topics/topik",
"data": {
"title": "This is a Firebase Cloud Messaging Topic Message!",
"content-text": "This is a Firebase Cloud Messaging Topic Message!"
}
}
or
Body:
{
"to": "/topics/topik",
"data": {
"message": "This is a Firebase Cloud Messaging Topic Message!"
}
}
the result on send action is something like this
{
"message_id": 7150560334538835864 (SUCCESS!)
}
but no notification arrives in any device. I tried to debug the onReceive method, but nothing happens.
Any idea?
Are you trying to send data-messages or notification-messages?
see: https://firebase.google.com/docs/cloud-messaging/concept-options#notifications_and_data_messages
If you want to send notification-messages (the type of messages sent by the notification console)
the payload you wrote in the question is wrong. Try replacing data with notification:
Body:
{
"to": "/topics/topik",
"notification": {
"title": "Hello",
"body": "This is a Firebase Cloud Messaging Topic Message!"
}
}
I'm using firebase console and can send only Notification messages using it.
Is there a way to send data messages using the same?
The Firebase Notifications Console can only be used to send notification messages. It cannot be used to send data messages.
See the table in message types in the Firebase documentation:
Notification message
Use scenario: FCM automatically displays the message to end-user devices on behalf of the client app. Notification messages have a predefined set of user-visible keys.
How to send:
Use your app server and FCM server API: Set the notification key. May have optional data payload. Always collapsible.
Use the Notifications console: Enter the Message Text, Title, etc., and send. Add optional data payload by providing Custom data in the Notifications console. Always collapsible.
Data message
Use scenario: Client app is responsible for processing data messages. Data messages have only custom key-value pairs.
How to send:
Use your app server and FCM server API: Set the data key only. Can be either collapsible or non-collapsible.
You can test both notification message and data message using Postman(rest client for testing http request).See screen shots:
In header pass:
key:Content-Type, value:application/json
key:Authorization:key=<Server key>
Please look here: Firebase push notifications update DB, my post from June.
In conclusion, you need send HTTP POST request to https://fcm.googleapis.com/fcm/send
https://fcm.googleapis.com/fcm/send
Content-Type:application/json
Authorization:key=AIzaSyZ-1u...0GBYzPu7Udno5aA
{ "data": {
"score": "5x1",
"time": "15:10"
},
"to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1..."
}
You can now send notification message via the console. Note that it is different from data messages; notification messages only trigger the onMessageReceived callback when the app is in the foreground.
They are inside the advanced options tab on the compose message screen.
Just expand it and type your key/value map.
These will be included into the data field of the notification.
Firebase allows us to send messages by accepting our post requests:
{ "data": {
"score": "5x1",
"time": "15:10"
},
"to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1..."
}
However, all examples which are shown in their docs are for specific users. That requires their registration keys.
In Firebase Dashboard we can send messages to devices by choosing specific platform.
What kind of parameter is required in order to send message for specific application package?
for example: kz.mycompany.myapp
With Firebase Cloud Messaging, you have a few ways to address to whom messages are sent:
to a specific device
to a device group
to a topic
The Firebase Notifications tab in the new Firebase Console uses either #1 or #2 to send to groups of users.
But it looks like what you are looking for can be most easily accomplished by sending to a topic:
FirebaseMessaging messaging = FirebaseMessaging.getInstance()
messaging.subscribeToTopic("package_kz_mycompany_myapp");
Then your server-code can push messages to this topic to have it reach all devices that have subscribed to the topic:
var topic = '/topics/package_kz_mycompany_myapp';
var data = { "data": {
"score": "5x1",
"time": "15:10"
},
to : topic };