【问题标题】:Firebase cloud messaging does not work after successfully sendToDevice成功 sendToDevice 后 Firebase 云消息传递不起作用
【发布时间】:2018-01-09 15:20:39
【问题描述】:

我使用 Firebase Cloud Functions 通过 Cloud Messaging 向 iOS 设备发送通知。 sendToDevice 方法效果很好,并且在 Firebase 函数日志中返回成功承诺,没有任何错误或警告,但在 iOS 上,不显示通知。如果我从 Firebase 通知仪表板发送通知,效果很好,并且通知会显示在设备上。我不知道我在哪里可以有错误。即使像我说的那样工作,错误也可能在 iOS 应用程序端?

Firebase 配置:

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

部分云端功能:

APP.services.firebase.admin.database().ref('tokens/' + userId).once('value', function(snapshot) {

    var userDevicesTokens = [];

    snapshot.forEach(function(childSnapshot) {
        userDevicesTokens.push(childSnapshot.key)
    });

    if (userDevicesTokens.length === 0) {
        console.warn('No tokens for user');
        return;
    }

    var payload = {
        data: {
            title: options.title,
            text: options.text,
            type: options.type,
        }
    };
    APP.services.firebase.admin.messaging().sendToDevice(userDevicesTokens, payload)
        .then(function(response) {

            console.log("Successfully sent message:", response);

        })
        .catch(function(error) {
            console.log("Error sending message:", error);
        });

})

Firebase 云函数日志:

11: 52: 43.952 AM info newChatMessageTrigger
Successfully sent message: {
    results: [{
        messageId: '0:15016....'
    }],
    canonicalRegistrationTokenCount: 0,
    failureCount: 0,
    successCount: 1,
    multicastId: 589....
}

11: 52: 22.760 AM outlined_flag newChatMessageTrigger
Function execution took 604 ms, finished with status: 'ok'

11: 52: 22.252 AM outlined_flag newChatMessageTrigger
Billing account not configured.External network is not accessible and quotas are severely limited.Configure billing account to remove these restrictions

11: 52: 22.252 AM outlined_flag newChatMessageTrigger
Function execution started

【问题讨论】:

  • 您是否尝试过发送notification 消息负载?

标签: javascript node.js firebase firebase-cloud-messaging google-cloud-functions


【解决方案1】:

我写信给 Firebase 支持,问题出在消息类型上。在 Firebase 中,我们有两种类型的消息:数据和通知。在这种情况下,我们应该使用类型通知 (see documentation)。

有效负载应如下所示:

var payload = {
    notification: {
        title: options.title,
        body: options.text,
    },
    data: {
        type: options.type,
    }
};

【讨论】:

    【解决方案2】:

    我认为这是您的有效负载的问题。

    使用您的 firebase 函数尝试此有效负载一次,如果您在 iOS 上收到通知,请告诉我。

    var payload = {
            data: {
                title: "Welcome to My Group",
                message: "You have new messages"
            }
        };
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-19
    • 2020-08-14
    • 1970-01-01
    相关资源
    最近更新 更多