Firebase Fcm Silent Push Notifications for iOS

Firebase FCM silent push notifications for iOS

Remove "notification" key value pair and add "content_available": true

It will look like this

{ 
"to" : "...",
"priority": "high",
"content_available": true,
"data" : {
....
}
}

This should make it a silent APNS and you need to handle with corresponding APNS delegate methods.

You will need to handle this through delegates
Refer this firebase documentation for details: https://firebase.google.com/docs/cloud-messaging/concept-options

how to send silent notification / background notification using Firebase Cloud Messaging?

Based on the error message that you are receiving, you should remove the apps property since data and notification properties are considered to be valid, as per the documentation.

Now, in regards to the the payload that you found elsewhere, this refers to the HTTP syntax used to pass messages from your app server to client apps via Firebase Cloud Messaging using the FCM legacy HTTP API. You can refer to the the documentation to learn more about the new HTTP v1 API.

To answer your question, when you are using a Cloud Function with Node.js runtime to send notifications using the sendToDevice(token, payload, options) method, you will need to pass the contentAvailable in the function's options parameter.

The contentAvailable option does the following: When a notification or message is sent and this is set to true, an inactive client app is awoken, and the message is sent through APNs as a silent notification and not through the FCM connection server.

Therefore, your Cloud Function might look something like this:

const userToken = [YOUR_TOKEN];

const payload = {
"data": {
"story_id": "story_12345"
},
"notification": {
"title": "Breaking News"
}
};

const options = {
"contentAvailable": true
};


admin.messaging().sendToDevice(userToken, payload, options).then(function(response) {
console.log('Successfully sent message:', response);
}).catch(function(error) {
console.log('Error sending message:', error);
});

Can I send a silent push notification from a Firebase cloud function?

I figured out how to modify my code to successfully send a silent notification. My problem was that I kept trying to put content_available in the payload, when it really should be in options. This is my new code:

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);

exports.sendNotifications = functions.database.ref('/events/{pushId}').onWrite(event => {
const id = event.params.pushId

const payload = {
data: {
title: 'An event has occurred!',
body: 'Please respond to this event.',
event_id: id
}
};

const options = {
content_available: true
}

return admin.messaging().sendToTopic("events", payload, options);
});

I successfully received the silent notification on my iOS device after implementing application:didReceiveRemoteNotification:fetchCompletionHandler and userNotificationCenter:willPresent:withCompletionHandler.

Send silent push notification from Firebase console

There is no way to send notifications different from the standard kind from the Firebase Console.

A quite convenient way is to use Postman or curl with a set Authorization Header.

curl -H "Content-type: application/json" -H "Authorization:key=<YOUR-API-KEY>"  -X POST -d '{ "data": { "foo": "1","bar": "2"},"to" : "<YOUR-DEVICE-TOKEN>"}' https://fcm.googleapis.com/fcm/send


Related Topics



Leave a reply



Submit