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

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!

Related

Flutter send firebase notification to token

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>"
}
}

MulticastMessage with different badge number for every user

I have set in my server side application the FCM service and I have been sending notifications (one by one) and has worked as expected.
Now, I'm trying to send notifications using MulticastMessage. It works good, devices are receiving notifications properly. However I don't know how to set a different badge number for every user.
I'm using the addAllTokens method to set the devices, and I was looking something like addAllBadges to send a list of badges corresponding to every device I insert in addAllTokens, but I can not find a solution. Is it possible? How can I do that?
Thank you very much!
You can achieve this by doing something like.....
First of all you need to query your DB to get the device token and badge count respectively
So say like you have and array
const badgeCounts = [{
device_token:'aaaaaaaaaaaaaaaaaaaaaa',
badge_count: 1
},{
device_token:'bbbbbbbbbbbbbbbbbbbbbb',
badge_count: 2
},{
device_token:'cccccccccccccccccccccc',
badge_count: 3
},{
device_token:'dddddddddddddddddddddd',
badge_count: 4
}]
Now you can map over this array and compose array of fcm messages, something like below :-
const fcmMessages = [];
badgeCounts.forEach((data) => {
fcmMessages.push({
token: data.device_token, //device token
apns: {
payload: {
aps: {
alert: {
title: "your title",
body: "your body",
},
badge: data.badge_count, // badge
contentAvailable: true,
},
},
},
data: {
// any payload goes here...
},
notification: {
title: "your title",
body: "your body",
},
});
});
/// in firebase messiging you can do like
messaging.sendAll(fcmMessages);
refer doc https://firebase.google.com/docs/reference/admin/node/admin.messaging.Messaging-1#sendall
As far as I know the message to each user in a multicast message is exactly the same. So if you need to show a different badge count, you'll need to do separate messages for these. But all recipients with the same content/badge count could be part of the same multicast message.

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"
}
}
}
}
}

Send push notification via REST API in Parse.com {"code":115,"error":"Missing the push data."}

When i am trying to test the REST API provided by Parse.com with Fiddler, it return an error. please find the enclosed screen capture. I can send the push notification on parse.com. but not from REST API.
Screenshot here:
http://s24.postimg.org/stwspcyir/image.jpg
http://s9.postimg.org/5r3guagh9/image.jpg
=== Follow Up ===
found the correct syntax of the JSON for push notification:
{"data": { "alert": "test message", "sound": "", "badge": "Increment" },"channel":"abc"}
but i found that if the channel is empty, the notification will not work, all i have to do is create channels for the device..... is it true ??
Found the answer finally:
if you have channels, provide the channel name:
{"data": { "alert": "321213", "sound": "", "badge": "Increment" },"channel":"channel name"}
if there is no channels (default no channel), send to all devices:
{"data": { "alert": "321213", "sound": "", "badge": "Increment" },"where": { "deviceType": "ios" }}

Resources