【问题标题】:Only receiving the last message sent (Firebase Cloud Messaging)仅接收发送的最后一条消息(Firebase 云消息传递)
【发布时间】:2020-02-26 06:04:54
【问题描述】:

我试图使用 firebase admin sdk 从 firebase 云功能向主题发送多条消息。但是,如果设备没有连接到网络,那么我打开网络连接我只会收到我在我的 android 应用程序上的 onMessageReceived() 方法中发送的最后一条消息。我想接收设备未连接到互联网时发送的所有消息。

我的云功能代码:

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

exports.showNotification = functions.https.onCall((data,context) => {

  var topic = 'weather';

  var message = {
    data: {
      title: 'This is title',
      description: getRandomString(15)
    },
    topic: topic,
    android : {
        ttl : 86400
    }
  };

  // Send a message to devices subscribed to the provided topic.
  admin.messaging().send(message)
    .then((response) => {
      // Response is a message ID string.
      console.log('Successfully sent message:', response);
      return response;
    })
    .catch((error) => {
      console.log('Error sending message:', error);
    });

});

【问题讨论】:

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


    【解决方案1】:

    可调用函数必须从函数回调的顶层返回一个承诺,该回调解析为要发送给客户端的数据。现在,您的函数什么也不返回,这意味着它立即终止并且什么也不返回。 return response 代码实际上只是从 then 回调函数返回一个值,而不是顶级函数。试试这个,它应该将该值从函数传播到客户端。

      return admin.messaging().send(message)
        .then((response) => {
          // Response is a message ID string.
          console.log('Successfully sent message:', response);
          return response;
        })
        .catch((error) => {
          console.log('Error sending message:', error);
        });
    

    在函数代码中正确处理promise非常重要,否则它们可能根本不起作用。

    【讨论】:

    • 这并没有解决我的问题。 onMessageReceived() 像以前一样在扩展 FirebaseMessagingService 的类中被调用一次,即使向主题发送了多个数据有效负载。
    猜你喜欢
    • 2017-04-05
    • 2021-07-06
    • 2020-03-15
    • 2017-02-07
    • 2021-12-02
    • 1970-01-01
    • 2023-03-04
    • 2020-07-28
    • 1970-01-01
    相关资源
    最近更新 更多