1

我使用Firebase云端函数通过Cloud Messaging向iOS设备发送通知。 sendToDevice方法工作良好,并且返回成功承诺,但Firebase函数日志中没有任何错误或警告,但在iOS上不显示通知。如果我通过Firebase通知信息中心发送通知,该通知很有用,并且会在设备上显示通知。我不知道我在哪里可以有bug。这个错误可能在iOS应用程序端,即使它像我说的那样工作?成功sendToDevice后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); 
     }); 

}) 

火力地堡云功能登录:

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 
+0

您是否尝试发送'notification'消息有效呢? –

回答

2

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

用您的Firebase功能尝试一次这个有效载荷,如果您在iOS上收到通知,请告诉我。

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

它仍然不起作用。相同的日志像早些时候 – Damian

+0

检查我的答案[这里](https://stackoverflow.com/questions/45428888/how-do-i-give-notification-every-time-user-get-registered-to-my-app-使用云/ 45429100?noredirect = 1个#comment77819438_45429100)。考虑到用户只有一个设备令牌,我已收到通知iOS的此云功能。 – Surjeet

+0

单个令牌不起作用。 – Damian

0

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

有效载荷应该是这样的:

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