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

Suf*_*tha 5 firebase typescript google-cloud-functions expo

我正在开发基于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 {
      console.log(snapshot.val())
    }
  })
  .then(() => {
    for (var token in allTokens){
      messages.push({
        to: token,
        sound: 'default',
        title: 'NXET',
        body: ' NEW ACTIVITIES ADDED!'
      })
    }
  })
  .then(() => {
    let chunks = expo.chunkPushNotifications(messages)

    async (chunks) => {
      for (let chunk of chunks) {
        try {
          await expo.sendPushNotificationsAsync(chunk);
        } catch (error){
          console.error(error);
        }
      }
    }

    return res.status(200).send('SUCCESS - NOTIFICATIONS SENT!!')

  })
  .catch(() => {
    return res.status(403).send('Forbidden!')
  });

  return res.status(200).send('Function Executing')

})
Run Code Online (Sandbox Code Playgroud)

package.json

{

"name": "functions",
  "scripts": {
    "build": "tsc",
    "serve": "npm run build && firebase serve --only functions",
    "shell": "npm run build && firebase functions:shell",
    "start": "npm run shell",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log"
  },
  "main": "lib/index.js",
  "dependencies": {
    "expo-server-sdk": "^2.3.3",
    "firebase-admin": "~5.12.0",
    "firebase-functions": "^1.0.1"
  },
  "devDependencies": {
    "typescript": "^2.5.3"
  },
  "private": true
}
Run Code Online (Sandbox Code Playgroud)

任何帮助将非常感激。

Chr*_*ris 2

快速浏览一下 Expo 服务器 sdk,它看起来像是isExpoPushToken一个类方法而不是实例方法,因此您应该调用类上的方法Expo而不是实例上的方法expo(注意大写与小写)。

来自Expo 服务器 sdk github 页面

// Check that all your push tokens appear to be valid Expo push tokens
if (!Expo.isExpoPushToken(pushToken)) {
  console.error(`Push token ${pushToken} is not a valid Expo push token`);
  continue;
}
Run Code Online (Sandbox Code Playgroud)