使用Firebase Cloud Function iOS推送通知
尝试通过Firebase云功能发送远程推送通知.我一直关注的资源通过sendToDevice
方法实现此目的,该方法将String作为参数.来自GitHub的资源称其为设备通知令牌",当用户同意在应用程序中接收通知时将检索该设备. Firebase称其为来自客户端FCM SDK的注册令牌".这里的输入应该是什么,以及如何检索?
Trying to send remote push notifications through firebase cloud functions. Resources I've been following achieves this through sendToDevice
method, which takes a String as an argument. A resource from GitHub says its a "device notification token" that is retrieved when user agrees to receive notifications in app. Firebase says its a "registration token that comes from the client FCM SDKs". What should be the input here, and how to I retrieve it?
// Send notification to device via firebase cloud messaging.
// https://firebase.google.com/docs/cloud-messaging/admin/send-messages
// https://github.com/firebase/functions-samples/blob/master/fcm-notifications/functions/index.js
//
admin.messaging().sendToDevice(request.query.tokenId, payload).then(response => {
response.results.forEach((result, index) => {
const error = result.error
if (error) {
console.log("Failure sending notification.")
}
});
});
您需要接收当前注册令牌.
注册令牌是通过FIRMessagingDelegate方法传递的 消息传递:didReceiveRegistrationToken :.这个方法叫做 通常每个应用以FCM令牌开头一次.当这种方法是 被称为,这是进行以下操作的理想时间:
Registration tokens are delivered via the FIRMessagingDelegate method messaging:didReceiveRegistrationToken:. This method is called generally once per app start with an FCM token. When this method is called, it is the ideal time to:
- 如果注册令牌是新的,请将其发送到您的应用程序服务器(建议实施服务器逻辑以确定是否已注册令牌. 令牌是新的.)
- 为主题注册注册令牌.仅对于新订阅或用户具有以下情况时才需要 重新安装该应用.
- If the registration token is new, send it to your application server (it's recommended to implement server logic to determine whether the token is new).
- Subscribe the registration token to topics. This is required only for new subscriptions or for situations where the user has re-installed the app.
因此,您必须在应用程序中保留此令牌,将其存储在Cloud Function可以保留的位置(传统上是实时数据库),并在函数运行时进行查询.
So, you'll have to get a hold of this token in your app, store it somewhere that the Cloud Function can get a hold of (traditionally, Realtime Database), and query for it at the time the function runs.