我使用 Firebase Cloud Functions 通过云消息向 iOS 设备发送通知。sendToDevice
方法效果很好,并且可以在Firebase Functions日志中返回成功承诺,而不会显示任何错误或警告,但在iOS上,不会显示通知。如果我从 Firebase 通知信息中心发送通知,效果很好,并且通知会显示在设备上。我不知道我在哪里会有错误。错误可能出在iOS应用程序端,即使它像我说的那样工作?
火力基本配置:
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 Cloud Functions 日志:
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
我写信给Firebase支持,问题出在消息类型上。在Firebase中,我们有两种类型的消息:数据和通知。在这种情况下,我们应该使用类型通知(参见文档(。
有效负载应如下所示:
var payload = {
notification: {
title: options.title,
body: options.text,
},
data: {
type: options.type,
}
};
我认为这是您的有效载荷的问题。
使用Firebase功能尝试一次此有效负载,如果您在iOS上收到通知,请告诉我。
var payload = {
data: {
title: "Welcome to My Group",
message: "You have new messages"
}
};