Firebase Cloud Messaging - iOS Badges - ios

I want my Cloud Function to send a push notification to a specific topic, which works just fine. However, when I'm adding the "badge" field, the following error occurs:
Unknown name "badge" at 'message.notification': Cannot find field.
What am I supposed to do if I want to specify a badge number for iOS devices? The code I have looks like follows:
exports.onAddLog = functions.database.ref("/NodesLog/{id}").onCreate((change, context) => {
const payload = {
"topic": "Channel123",
"notification": {
"title": "Test-Title",
"body": "Test-Body",
"badge": "1"
},
"data": {
"test": "test",
"notId": "123"
}
}
const promise : Promise<any> = admin.messaging().send(payload)
.catch(error => console.error("Error on send: " + error))
.then(sendSuccess => console.log("Notification send "))
.catch(() => console.error("What happened?"))
return promise;
})

The problem is that badge is iOS field and it should be in another place as Ali said it is in "aps" object. But what he didn't say is where aps should be.
for example this should be your message
{
"message":{
"topic": "Channel123",
"notification": {
"title": "Test-Title",
"body": "Test-Body",
"badge": "1"
},
"data": {
"test": "test",
"notId": "123"
},
"apns": {
"payload": {
"aps": {
"badge": 5
}
}
}
}
}
I took the example from here

try to add aps node containing badge property with your payload
const payload = {
...
"aps":{
"alert":"test alert",
"badge":5,
"sound":"default"
}
}

The following payload worked for me on both Android and iOS:
payload = {
notification: {
"title": "Message Title",
"body": "Message Body",
"click_action": ".MainActivity",
"badge": "1",
},
data: {
"my_message_type": "foo",
"my_id": "bar",
},
};

const payload = {
token: "FCMToken",
notification: {
title: "title",
body: "body",
},
apns: {
payload: {
aps: {
badge: 5,
sound: "default",
},
},
},
};

Related

Silent push notification has a sound in iOS with FCM, How to fix?

I've implemented Silent push notification, but it has a sound like the normal ones. Help me to correctly implement this.
Here's the payload that we're sending from Backend to FCM:
{
"notification": {
"messageId": "24d0f68a24d"
},
"content_available": true,
"data": {
"requestId": "231DDD",
"messageId": "f68a24d",
"event": "REMOVE_NOTIFICATION",
"message": ""
},
"apns": {
"headers": {
"apns-push-type": "background",
"apns-priority": 5
}
},
"to": "FCM_Token",
"priority": "high"
}
To make this silent, you have to remove following part.
"notification": {
"messageId": "24d0f68a24d"
},

Unable to send category in aps object

I am trying to send push notification to ios device via firebase and also I need to send category object with the aps payload so that I can create notification with action buttons. I am using the below payload specified in firebase documentation, but I am unable to receive the category object in the aps payload object in the front end. Any help would be appreciable. Thanks in advance.
{
"message": {
"token": "f8vYHFfogAA:APA91bG7cZlo7Iimv1UtD0atqTlBRKYsGTU2pdWQmfELpdG61E4_TstS8MNHvHgNJW_dECi8YxD0REiDjxbkBVK4Ey9MNJ4MTxBVJpa48sirKb2a93QyfrpRwDMB9_j8UaNexP5rQ7Ci",
"notification": {
"title": "Match update",
"body": "Arsenal goal in added time, score is now 3-0"
},
"android": {
"ttl": "86400s",
"notification": {
"click_action": "OPEN_ACTIVITY_1"
}
},
"apns": {
"headers": {
"apns-priority": "100"
},
"payload": {
"aps": {
"category": "NEW_MESSAGE_CATEGORY"
}
}
},
"webpush": {
"headers": {
"TTL": "86400"
}
}
}
}

InvalidRegistration on sending push to iOS

I had a problem. I try to send notification to iOS app with sending post request to https://fcm.googleapis.com/fcm/send
Request:
{
"notification":
{
"body": "test",
"sound" : "default",
},
"data":
{
"id": "",
"type": "",
},
"project_id": "my-app-id",
"registration_ids": [ "6d0a9931d0b5145e69c2aa5402b4acbf46b6da7193c659c5858ff4c3baa417d5" ],
"time_to_live" : 3,
"priority" : "high"
}
and every time I get
responce:
{
"multicast_id": 6755836923824168435,
"success": 0,
"failure": 1,
"canonical_ids": 0,
"results": [
{
"error": "InvalidRegistration"
}
]
}
But when I use NWPusher (https://github.com/noodlewerk/NWPusher) everything is Ok, I get the push notification
image of success sending push notification

Urban Airship showing wrong JSON payload

I am trying to send test push from Urban Airship portal. I have got channel ID of device, created JSON payload. However when I am trying to send, it always show error, Sorry, this doesn't look like valid JSON. Below is my Payload data, please help me in this.
{
"audience": {
"ios_channel": "938a3a93-1a5f-466c-923b-827ef4b0a75b"
},
"notification": {
"aps": {
"alert": {
"body": "Sample",
"title": "Sample"
}
}
"ios": {
"badge": 3
}
},
"device_types": [
"ios"
]
}
This valid , you forget a comma , you may use https://jsonlint.com for such things
{
"audience": {
"ios_channel": "938a3a93-1a5f-466c-923b-827ef4b0a75b"
},
"notification": {
"aps": {
"alert": {
"body": "Sample",
"title": "Sample"
}
},
"ios": {
"badge": 3
}
},
"device_types": [
"ios"
]
}

FCM rich push notification payload for iOS

I am using FCM for my project. It's have rich push notification for a type. I tried to modified most of possible ways to get push from FCM. I got obly ordinary push from FCM, not with image.
I am also check with APNS same coding using push try. I got what expected design for push notification.
Here my APNS payload
{
"aps": {
"alert": "Enter your message",
"badge": 1,
"sound": "default",
"content-available": 1,
"mutable-content": 1
},
"mediaUrl": "https://upload.wikimedia.org/wikipedia/commons/thumb/2/2a/FloorGoban.JPG/1024px-FloorGoban.JPG"
}
Here FCM payload
{
"to": "dWB537Nz1GA:APA91bHIjJ5....",
"data":
{
"message": "Offer!",
"mediaUrl": "https://upload.wikimedia.org/wikipedia/commons/thumb/2/2a/FloorGoban.JPG/1024px-FloorGoban.JPG"
},
"notification":
{
"body": "Enter your message",
"sound": "default",
"content-available": 1,
"mutable-content": 1
}
}
Also I am need category more details about payload in FCM
Am I missing any setting in fire-base console or is that from payload.
The mutable-content and content-available in your FCM payload is incorrect. It should be formatted as mutable_content and content_available. Both are boolean and must also be outside the notification parameter. Like so:
{
"to": "dWB537Nz1GA:APA91bHIjJ5....",
"content_available": true,
"mutable_content": true,
"data":
{
"message": "Offer!",
"mediaUrl": "https://upload.wikimedia.org/wikipedia/commons/thumb/2/2a/FloorGoban.JPG/1024px-FloorGoban.JPG"
},
"notification":
{
"body": "Enter your message",
"sound": "default"
}
}
For the counterpart of category in FCM, you should use click_action:
The action associated with a user click on the notification.
Corresponds to category in the APNs payload.
This worked for me. The accepted answer seems to have some unnecessary information.
{
"to" : "devicekey OR /topics/sometopic",
"mutable_content": true,
"data": {
"mymediavideo": "https://myserver.com/myvideo.mp4"
},
"notification": {
"title": "my title",
"subtitle": "my subtitle",
"body": "some body"
}
}

Resources