I am using Firebase Cloud Messaging to send notifications for my iOS app. I would like to add a vibration and sound when the device receives a notification. I've tried the following structure for the message:
var message = {
"notification": {
"title":"Ma Cherie",
"body":messageText
},
"sound":"default"
topic: topic
};
But I get the following error:
Error sending message: { Error: Invalid JSON payload received. Unknown name "sound" at 'message.notification': Cannot find field.
I have also tried:
var message = {
"notification": {
"title":"Ma Cherie",
"body":messageText
},
"apns": {
"payload": {
"aps": {
"sound": "default"
}
}
},
topic: topic
};
but I get the same error. How do I add a sound to the notification (and a vibration and badge)?
Try replacing
"sound":"default"
with
"ios": { "notification": { "sound": 'default' } }
I had similar issue on android and this worked for me.
Please refer to Jaime's Post for details.
Related
We are developing an Ionic app for Android and iOS that has Push Notifications. We send these notifications via Firebase Cloud Messaging
On iOS we have this problem (we have accepted Notifications Permission for the app):
When the app is in foreground, we get the notification instantly (we get it inside the app, not with a card).
When the app is closed or in background it seems like we do not get the notification. However wehn we open or resume the app, we get the notification as is it was recieved with the app in foreground. Our hypothesis is that we are getting silent notifications.
(When we send Firebase test notification we get the same behaviour).
We have the next configurations:
Xcode: Capabilities -> Background Modes -> Remote Notifications (shows one check)
Xcode: Capabilities -> Push Notifications (shows two checks)
Apple Developer: Identifiers -> my app -> Push Notifications (with certificates for both dev and prod)
Apple Developer: Keys -> .p8 key with "Apple Push Notifications service (APNs)"
Firebase: my app -> Configuration -> Cloud Messaging (.p8 file uploaded with key and team IDs)
This is one of the several notifications we have tried to send via Firebase POST API, which combines almost everything we have read while researching this problem. (we also send required Firebase HTTP headers)
{
"to": "<firebase_token>",
"notification": {
"body": "NOTIFICATION BODY",
"title": "NOTIFICATION TITLE",
},
"apns": {
"headers": {
"apns-push-type": "alert",
"apns-expiration": 0,
"apns-priority": 5,
"apns-topic": "<my_app>"
},
"payload": {
"alert": {
"title": "NOTIFICATION TITLE",
"body": "NOTIFICATION BODY"
},
"aps": {
"content-available": 1,
"alert": {
"title": "NOTIFICATION TITLE",
"body": "NOTIFICATION BODY"
}
},
"sound": "default",
"content-available": 1
}
},
"data": {
"field": "1",
"type": "CHAR"
}
}
Despite this POST request may be incorrect for bacground notifications, we believe that would not be the problem, as Firebase test notifications also fail to show when on background.
We finally were able to get notifications with app on background and on foreground.
The payload for Firebase is:
{
"to": "<firebase token>",
"notification": {
"title": "TITLE",
"body": "BODY"
},
"data": {
"title": "TITLE",
"body": "BODY",
"extraField1": "extra value 1",
"extraField2": "extra value 2"
},
"apns": {
"headers": {
"apns-topic": "<my-app>",
"apns-push-type": "background",
"apns-priority": 10
}
}
}
It looked like there were an problem with .p12 certificates after updating to iOS 13 that we solved using a .p8 cert. After changing the certificates at Firebase the background notifications started to be recieved, but foreground stopped for the first hours.
I am not sure, But it seems like your payload structure is not proper.
Please refer below sample
{
"to" : "FCM TOKEN",
"notification" : {
"body" : "Body of Your Notification",
"title": "Title of Your Notification"
},
"data" : {
"body" : "Body of Your Notification in Data",
"title": "Title of Your Notification in Title",
"key_1" : "Value for key_1",
"key_2" : "Value for key_2"
}
}
If you're working with normal APNS then no need to pass content-available
Look for the message in XCode logs. Here is what I got, see below:
In the js code to access body, use data.aps.alert.body
this.fcm.onNotification().subscribe(data => {
console.log("notifiation data", data);
alert(data.aps.alert.body);
Message ID 1: 1593974546767029
{
aps = {
alert = {
body = "Hi, this is ios and android test 6!!!";
title = " iOS and Android Test";
};
badge = 1;
sound = default;
};
"gcm.message_id" = 1593974546767029;
"gcm.n.e" = 1;
"gcm.notification.sound2" = default;
"google.c.a.c_id" = 2904766990309581961;
"google.c.a.e" = 1;
"google.c.a.ts" = 1593974546;
"google.c.a.udt" = 0;
"google.c.sender.id" = 291488852090;
}
I have implemented an app with custom notification using firebase. I am able to receive custom notifications using device token from the pusher, pushtry, But if I send the same payload using FCM ID from postman then not receiving the notification.
I have tried other payloads also but just receiving the default notification and sometimes only sound for some payloads, but not getting custom one.
I am using the payloads like this for pusher and pushtry:
(received proper notifications)
{
"aps":{
"alert":"dasdas",
"badge":1,
"sound":"default",
"category":"CustomSamplePush",
"mutable-content":"1"
},
"urlImageString":"https://res.cloudinary.com/demo/image/upload/sample.jpg"
}
the same payload not working for firebase
Is there any proper payload for that and is there any need to do changes from my ios code?
thanks in advance.
For custom notification using firebase the payload should be like this.
{
"to": "FCM ID",
"content_available":true,
"mutable_content": true,
"data": {
"message": "Offer!",
"urlImageString":"https://res.cloudinary.com/demo/image/upload/sample.jpg",
},
"notification": {
"body": "Update Your App New Version is available",
"sound": "default",
"click_action": "Your app category from notification plist"
}
}
Your payload should be like:
{
"to": "APA91bHun4MxP5egoKMwt2KZFBaFUH-1RYqx...",
"notification": {
"body": "dasdas",
"title": "Portugal vs. Denmark",
"icon": "myicon"
},
"data": {
"Nick": "Mario",
"Room": "PortugalVSDenmark"
}
}
Having all data under aps does not trigger any notification in iOS even though Firebase returns a successful request because the forwarded APNS payload is not correct. Besides the proper way should be to follow the GCM or FCM payload recommendations, which is to use both notification for the notification message and data for custom key/value pairs.
When FCM Send data to APNS it convert it into APNs payload. It set values of notification in aps tag i.e.
{
"aps" : {
"alert" : {
"title" : "Portugal vs. Denmark",
"body" : "Portugal vs. Denmark",
}
},
"Nick" : "Mario",
"Room" : "PortugalVSDenmark"
}
We are using Firebase for sending remote push notifications, we are in need to grouping received notification like WhatsApp (Ex: 25 new messages received). I have tried it by adding APNs in JSON, but it's not working. Here is my tried code:
{
"to" : "**FCM TOKEN**",
"priority" : "high",
"notification" :
{
"title" : "You have a new message",
"body" : "Test",
"badge" : 1
},
"apns":
{
"headers":
{
"apns-collapse-id":"***APNs ID***"
}
}
}
I'm receiving notification but not able to grouping it.apns-collapse-id which was taken from developer.apple.com.
I can send push notifications from Firebase Console Notifications to my iOS device, and it works perfectly being the app in foreground and background.
When I try to send them using Firebase-admin by NodeJS it only works when the app is in foreground, in background nothing happens.
I think that the communications between FCM-APNs are good because it works with the console.
This is my NodeJS code:
function sendFCM(registration_ids, data, collapseKey) {
const options = {
priority: "high",
collapseKey : collapseKey,
contentAvailable : true,
timeToLive: 60 * 60 * 24
};
const payload = {
data: data,
notification: {
title: "My title",
text: "My description",
sound : "default"
}
}
admin.messaging().sendToDevice(registration_ids, payload, options)
.then(function(response) {
console.log("Successfully sent message:", response);
})
.catch(function(error) {
console.log("Error sending message:", error);
});
}
What do you think that it's happening? Do you know some way to log the issue?
The Server Protocol documentation indicates the key for notification text is body, not text. See if this change makes a difference:
const payload = {
data: data,
notification: {
title: "My title",
body: "My description", // <= CHANGE
sound : "default"
}
}
I'm trying to implement notifications using Firebase. The notification is received correctly when the app is in the background or foreground. So, the basic mechanics are working.
Now I've added Content Extensions and Service Extensions to the app. The Content Extension works when I use a local notification, but the Firebase message payload seems incorrect as far as the optional fields are considered. Here is a link to an image of my console:
And here is the Firebase remote notification payload that comes across (with some of the long Google numbers edited for anonymity:
{
aps =
{
alert =
{
body = "Eureka! 11";
title = "Patient is not doing well";
};
};
category = provider-body-panel;
gcm.message_id = 0:149073;
gcm.n.e = 1;
google.c.a.c_id = 2825604;
google.c.a.e = 1;
google.c.a.ts = 149073;
google.c.a.udt = 0;
mutable-content = 1;
}
It appears that the "category" and "mutable-content" are not in the correct place. They should be in the aps payload.
How can I get those options to be in the payload so that my app can correctly parse it and connect it with the Content and Service Extensions?
To start off, I'm going to mention that there are two types of message payloads for FCM. notification and data. See the documentation here
When sending notifications through the Firebase Notifications Console, it will be treated as a notification payload. However, if you add in Custom Data, it will add it in the payload as a custom key-value pair.
For example, in your post, the FCM payload should look something like this:
{
"notification": {
"body" : "Eureka!",
"title": "Patient is not doing well"
},
"data": {
"category": "provider-body-panel",
"mutable-content" : true,
"click_action" : "provider-body-panel"
}
}
What's wrong?
click_action should be inside notification.
mutable-content should be mutable_content (notice the underscore) and should be on the same level as notification.
(this one I might've misunderstood, but) There is no category parameter for FCM, click_action already corresponds to it.
See the docs for the parameters here.
It it is currently not possible to set the value for click_action and mutable_content when using the Firebase Notifications Console. You'll have to build the payload yourself, something like this:
{
"to": "<REGISTRATION_TOKEN_HERE>",
"mutable_content" : true,
"notification": {
"body" : "Eureka!",
"title": "Patient is not doing well",
"click_action" : "provider-body-panel"
}
}
Then send it from your own App Server. You could also do this by using Postman or cURL
"mutable-content should be "mutable_content" (keyword for firebase server to send as mutable-content for IOS) as you mentioned in your post, I think you left out in edit.
Below is an example with also the corrected format for the data section in the json sent to the FCM server.
So update would be:
{
"to" : "YOUR firebase messaging registration id here",
"mutable_content":true,
"notification": {
"title": "Its about time",
"body": "To go online Amigo",
"click_action": "NotificationCategoryIdentifier ForYourNotificationActions"
},
"data":{
"customKey":"custom data you want to appear in the message payload"
"media-attachment":"mycustom image url",
"catalogID":"mycustom catalog for my custom app"
}
}
Update Firebase Admin SDK and use sendMulticast(payload) method
var admin = require("firebase-admin")
admin.initializeApp({
credential: admin.credential.applicationDefault(),
});
// Create a list containing up to 500 registration tokens.
// These registration tokens come from the client FCM SDKs.
const registrationTokens = [
'YOUR_REGISTRATION_TOKEN_1',
// …
'YOUR_REGISTRATION_TOKEN_N',
];
// See documentation on defining a message payload.
var message = {
notification: {
title: '$FooCorp up 1.43% on the day',
body: '$FooCorp gained 11.80 points to close at 835.67, up 1.43% on the day.'
},
tokens: registrationTokens,
apns: {
payload: {
aps: {
'mutable-content': true, // use single quote
'category': 'INVITE_CATEGORY' // use single quote
}
},
},
};
// Send a message to the device corresponding to the provided
// registration tokens.
admin.messaging().sendMulticast(message)
.then((response) => {
if (response.failureCount > 0) {
const failedTokens = [];
response.responses.forEach((resp, idx) => {
if (!resp.success) {
failedTokens.push(registrationTokens[idx]);
}
});
console.log('List of tokens that caused failures: ' + failedTokens);
}
});
Ref: https://firebase.google.com/docs/cloud-messaging/send-message#send_messages_to_specific_devices
This worked for me with Cloud functions with Node.js
const payload = {
notification: {
title: name,
body: messageText,
badge: "1",
mutable_content: "true"
},
data: {
type: "MESSAGE",
fromUserId: name,
attachmentUrl: imageUrl
}};