成功发送sendToDevice

时间:2017-08-02 11:53:43

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

我使用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

2 个答案:

答案 0 :(得分:3)

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

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

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

答案 1 :(得分:3)

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

Payload应如下所示:

/* in while() loop continue reading from file 1 until nothing is left to read */
while (bytesMoved = fread(buffer1, sizeof(buffer1), bufferSize1, fp1))
{

    /* transfer from buffer1 to buffer2 */
    for(i = 0; i < bytesMoved && i < bufferSize2; i++)
        buffer2[i] = buffer1[i];
    buffer2[i] = '\0';

    iterations = 1; /* this is just in case your buffer2 is super tiny and cannot store all from buffer1 */
    /* in while() loop continue writing to file 2 until nothing is left to write 
        to upgrade use strlen(buffer2) instead of bufferSize2 */
    while (bytesTransfered = fwrite(buffer2, sizeof(buffer2), bufferSize2, fp2))
    {    
        /* reset buffer2 & write again from buffer1 to buffer2 */
        for(i = bufferSize2 * iterations, j = 0; i < bytesMoved && j < bufferSize2; i++, j++)
            buffer2[j] = buffer1[i];
        buffer2[j] = '\0';
        iterations++;
    }
    /* mem reset buffer1 to prepare for next data transfer*/
}