小编Suf*_*tha的帖子

Firebase Cloud功能-触发Expo SDK向用户推送通知

我正在开发基于Expo的应用程序-我决定将Firebase用作后端,但遇到了阻塞问题,我需要向应用程序中的用户发送推送通知-但Expo不支持Firebase Cloud Messaging( FCM),除非我退出Expo项目并独立使用Android和ios。

为了克服这个问题,我决定将Expo Notification令牌存储在firebase中,然后在firebase中编写一个firebase云函数,以从数据库中获取所有这些令牌,并使用expo-server-sdk触发sendPushNotificationAsync()。

我知道要执行此方法,我的Firebase版本不能包含在Spark计划中-并非如此。

我决定在TypeScript中实施该解决方案,以便可以符合ES6 / ES7编码标准。

方法是-我触发对Firebase函数URL的获取请求,这将触发函数执行。使用下面的代码-当我对提供的google函数URL进行get请求时,出现错误:

@ firebase / database:FIREBASE警告:用户回调引发了异常。TypeError:expo.isExpoPushToken不是一次在admin.database.ref.once(/user_code/lib/index.js:24:18)的函数回调(/ user_code / node_modules / firebase-

尽管我已将其包含在要安装,导入和使用的package.json中,但我在做某事时似乎不正确,但是它似乎无法初始化和使用来自Expo导入的功能。

index.ts-由Firebase生成

import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';
import * as Expo from 'expo-server-sdk';

admin.initializeApp();
// Create a new Expo SDK client
let expo = new Expo();

exports.pushNotifications = functions.https.onRequest((req,res) => {
  if(req.method !== 'GET'){
    return res.status(403).send('Forbidden!')
  }

  let allTokens = [];
  let messages = [];

  admin.database().ref('/all_user_push_tokens/').once('value', (snapshot) => {
    if(expo.isExpoPushToken(snapshot.val())){
      allTokens.push(snapshot.val())
    } else …
Run Code Online (Sandbox Code Playgroud)

firebase typescript google-cloud-functions expo

5
推荐指数
1
解决办法
564
查看次数