【问题标题】:Push Notification Using Google Cloud Functions and Google Firebase使用 Google Cloud Functions 和 Google Firebase 推送通知
【发布时间】:2017-09-12 05:13:30
【问题描述】:

我有一个使用 Firebase 的 android Client ApplicationAdmin Application。每当用户在客户端应用程序中注册时,我都需要向管理应用程序发送推送通知。我正在尝试使用Cloud Functions for Firebase。我已经导出了这个函数,我也可以在 firebase 控制台上看到它。

这是我的 index.js

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);


exports.sendMessageToAdmin = functions.database.ref('/tokens/users/{userId}/{regToken}').onWrite(event => {

  if (event.data.previous.exists()) {
    return;
  }

  const userId = event.params.userId;
  const regToken = event.params.regToken;

  // Notification details.
    const payload = {
      notification: {
        title: 'You have a new User.',
        body: `${userId} is the id.`,
      }
    };
  return admin.messaging().sendToDevice(regToken, payload);
});

这是我在 firebase 的数据库结构:

如果我使用任何在线门户发送推送甚至 FCM 发送推送到管理应用程序以进行测试,我正在接收推送。但是这个 Cloud Function 并没有发送推送。有人可以指导我我在做什么错。

编辑

如果我将函数更改为以下,那么它可以工作。但我仍然想知道为什么上述功能不起作用。

exports.sendMessageToAdmin = functions.database.ref('/tokens/users/{userId}').onWrite(event => {


  if (event.data.previous.exists()) {
    return;
  }

  const userId = event.params.userId;
  var eventSnapshot = event.data;
  const regToken = eventSnapshot.child("regToken").val();

   Notification details.
    const payload = {
      notification: {
        title: 'You have a new User.',
        body: `${userId} is the id.`,
      }
    };
  return admin.messaging().sendToDevice(regToken, payload);
});

【问题讨论】:

  • 您收到的是成功响应还是错误响应?
  • 添加日志语句并在 Firebase 控制台的日志窗格中查找输出:console.log('userId:', userId, ' token: ', regToken);
  • 您的 Firebase 日志输出说明了什么?您可以在 Firebase 控制台的 Functions -> Logs 下找到它
  • userId: 123456793 token: regToken这是日志,缺少regToken值
  • 我猜,我以错误的方式访问 regToken 值。

标签: android firebase firebase-cloud-messaging google-cloud-functions


【解决方案1】:

在您的原始代码中,您有:

const regToken = event.params.regToken;

event.params.regToken 不返回 regToken 的值,它返回您引用中 wildcard path segment 的值。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-07-08
    • 2017-11-16
    • 2016-06-24
    • 1970-01-01
    • 1970-01-01
    • 2020-03-15
    • 2017-05-31
    • 2017-10-08
    相关资源
    最近更新 更多