Flutter send firebase notification to token - firebase

So i have a chat app and i want it so that i can get a notification once someone sends me a message. The thing is that i implemented firebase messaging and i can receive a notification if i send it from firebase. I want to add it such as once i press the send button i can directly send the notification to a token within the app for instance something like:
send notification{
to token : here the receiver token
title : the title of the notification
description : the description of the notification
}
is it possible to have something like this?

Just for helping,
If anyone wants to use REST POST API, here it is, use the Postman with below configuration
URL:
https://fcm.googleapis.com/fcm/send
Header:
"Content-Type": "application/json",
"Authorization": "key=<Server_key>"
BODY:
{
"to": "<Device FCM token>",
"notification": {
"title": "Check this Mobile (title)",
"body": "Rich Notification testing (body)",
"mutable_content": true,
"sound": "Tri-tone"
},
"data": {
"url": "<url of media image>",
"dl": "<deeplink action on tap of notification>"
}
}

Related

How to achieve time-sensitive flag in APNS JSON output via FCM?

iOS distinguishes between messages by UNNotificationInterruptionLevel. I would like to achieve that messages sent via FCM have the time-sensitive interruption-level.
Is this equivalent to just sending messages in FCM with high priority? Unfortunately it's not super clear to me from looking at the docs.
The interruption level is automatically handled by system, not by FCM. That's different than the high priority.
You should be able to use it as it is by following Apple's documentation. FCM supports passing down the interruption-level in the payload.
I achieved this by having this payload in my firebase console file:
var message = {
notification: {
title: "Notification Title",
body: `${initiatedUsername} sent you message`,
},
"data": {
"target_exec":"messaging"
},
"apns": {
"payload": {
"aps": {
"alert": {
"title": "Notification Title",
"body": `${initiatedUsername} sent you a message`
},
"badge": 1,
"sound": "default",
"interruption-level": "time-sensitive"
}
}
},
token: fcmToken,
};
You can change the token attribute to topic aswell, if you'd rather like to send a message to a topic.
Hope this might help somebody out here!

FCM registration token - is it possible to find token value in app data?

I need to get a FCM registration token value which is on my device - just only to test push notification on my device before publishing it to all users. I know that there is a way to get a token during registration and send it to server, but maybe it's possible to find it in some app data without additional implementation?
Just print token to console and test it using postman.
var token = await FirebaseMessaging.getInstance().getToken();
print("token= "+token);
Then copy the token from the console and post using this api:
URI: https://fcm.googleapis.com/fcm/send
METHOD: POST
HEADERS: {
'Content-Type': 'application/json',
'Authorization': 'key=YOUR_FIREBASE_SERVER_KEY'
}
BODY: {
"notification":{
"body": "TEST MESSAGE",
"title": "TEST MESSAGE",
},
"priority": "high",
"data": {
"id": "1",
"status": "done"
},
"registration_ids": "THE_TOKEN",
},
Don't forget to close your app before sending the fcm request.

The request was missing an Authentication Key with push notification in firebase

I'm trying to send a push notification via REST API Firebase by Postman for specific user I followed below request pattern:
POST request with below URL :
https://fcm.googleapis.com/fcm/send?key=**my Web API key**
and the Body is
{ "data":
{
"title": "Firebase notification",
"detail": "I am firebase notification. you can customise me. enjoy"
},
"to" : "USER UID"
}
but the problem is it returns The request was missing an Authentication Key.
Kindly follow the steps as per bellow :
API URL: https://fcm.googleapis.com/fcm/send/
Request Method Post
Add 2 Key-value pairs in Header section like this
Content-Type : application/json
Authorization: key=[Your server key] (Make sure that no space allowed)
For body choose raw data (JSON)
{ "data": { "title": "Firebase notification", "detail": "I am firebase notification. you can customise me. enjoy" }, "to" : "USER UID" }

Send messages to specific devices using new firebase API

I am writing a new server application (using .net) to send notifications to clients (mainly android devices), and I am using the new Http v1 api from firebase cloud messaging, I saw the following Post request body to send a notification for topics
{
"message": {
"topic": "news",
"notification": {
"title": "Breaking News",
"body": "New news story available."
},
"data": {
"story_id": "story_12345"
}
}
}
However how am I going to send a notification to specific device using FCM Id ?
Note: I had already implemented sending notification to single device using the legacy API.
There is documentation for the v1 HTTP API. Specifically, there is a section for sending messages to a specific device. You need to know the device token:
{
"message":{
"token" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"notification": {
"title": "Breaking News",
"body": "New news story available."
},
"data": {
"story_id": "story_12345"
}
}

Firebase Cloud Messaging sound error

I'm trying to send a notification using firebase api and the notification is sent successfully if I only have "title" and "body" in the notification JSON object. However, if I add "sound":"default" to the notification object, as described in the documentation, I get the following error:
"Invalid JSON payload received. Unknown name \"sound\" at 'message.notification': Cannot find field."
My JSON object is as follows:
{"message":{"token": token, "notification":{"title":"Test", "body":"Test message from server", "sound":"default"}}}
The appearance of message in your JSON indicates you are using the HTTP v1 API. The documentation you linked is for the legacy API.
The HTTP v1 API JSON to send a notification with sound for Android and iOS devices should be:
{
"message": {
"token": "your-token-value",
"notification": {
"title": "Test",
"body": "Test message from server"
},
"android": {
"notification": {
"sound": "default"
}
},
"apns": {
"payload": {
"aps": {
"sound": "default"
}
}
}
}
}

Resources