使用 Firebase Cloud Function iOS 推送通知 [英] Push notifications using Firebase Cloud Function iOS

查看:34
本文介绍了使用 Firebase Cloud Function iOS 推送通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试通过 Firebase 云功能发送远程推送通知.我一直在关注的资源通过 sendToDevice 方法实现了这一点,该方法将字符串作为参数.来自 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.")
            }
        });
    }); 

推荐答案

你需要将 FCM 集成到您的 iOS 应用中.注意接收当前注册令牌.

注册令牌通过 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:

  • 如果注册令牌是新的,将其发送到您的应用服务器(建议实现服务器逻辑以确定是否令牌是新的).
  • 为主题订阅注册令牌.这仅适用于新订阅或用户拥有重新安装了该应用.

因此,您必须在应用程序中获取此令牌,将其存储在 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.

这篇关于使用 Firebase Cloud Function iOS 推送通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆