Firebase云消息传递在成功发送到设备后无法工作

3
我使用Firebase云函数通过云消息传递发送通知到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

你尝试过发送“通知”消息负载吗? - AL.
2个回答

6
我向Firebase Support写信,问题出在消息类型上。在Firebase中,我们有两种消息类型:数据和通知。在这种情况下,我们应该使用通知类型(请参见文档)。
有效载荷应该是这样的:
var payload = {
    notification: {
        title: options.title,
        body: options.text,
    },
    data: {
        type: options.type,
    }
};

3

我认为这是与你的有效载荷有关的问题。

请尝试使用此有效载荷调用Firebase函数,然后告诉我是否在iOS上收到了通知。

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

请查看此处,我已经使用云函数实现了每当用户在我的应用程序中注册时将通知发送到 iOS 设备的功能,仅考虑一个用户的设备标记。 - Surjeet Singh
谢谢@Surjeet,这对我有用。参考:https://firebase.google.com/docs/cloud-messaging/admin/send-messages 除了格式密钥外,一切都运行良好。 - Rain

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接