Is it possible to send a silent APNs (iOS) remote notification from a Firebase Cloud Function? If so, how can this be done? I want to send data to iOS app instances when the app is not in the foreground, without the user seeing a notification.
I currently send a notification that can be seen by users:
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 = {
notification: {
title: 'An event has occurred!',
body: 'Please respond to this event.',
event_id: id
}
};
return admin.messaging().sendToTopic("events", payload);
});
I would like to be able to send that id
to the app without a visual notification.
There are two ways users can receive silent push notifications on Android. Users can long press on a notification to get an option to display notifications silently. Users can also enable silent notifications by heading to Settings > App & Notifications > Search for app and choose> Notifications, and turn it off.
It's definitely possible -- you don't have to use Firebase to deliver push notifications. You can roll out your own notification solution or consider a paid product such as Pushy (pushy.me) which does not rely on Firebase Cloud Messaging.
Users can choose to mute their notifications and receive silent push notifications instead of being alerted to every new push that arrives. They can activate this on iOS or Android from their individual settings pages. And developers or mobile marketers cannot override this setting once put into place.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With