我可以从Firebase云功能发送无声推送通知吗?
是否可以从 Firebase Cloud Function 发送静默APN(iOS)远程通知>?如果是这样,该怎么办?我想在应用程序不在前台时将数据发送到iOS应用程序实例,而用户不会看到通知.
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);
});
我希望能够在没有视觉通知的情况下将该id
发送到该应用.
I would like to be able to send that id
to the app without a visual notification.
我想出了如何修改代码以成功发送静默通知的方法.我的问题是,我一直试图将content_available
放在payload
中,而实际上却应将它放在options
中.这是我的新代码:
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);
});
实现application:didReceiveRemoteNotification:fetchCompletionHandler
和userNotificationCenter:willPresent:withCompletionHandler
后,我在iOS设备上成功接收了静默通知.
I successfully received the silent notification on my iOS device after implementing application:didReceiveRemoteNotification:fetchCompletionHandler
and userNotificationCenter:willPresent:withCompletionHandler
.