Dam*_*ian 2 javascript node.js firebase google-cloud-functions firebase-cloud-messaging
我使用 Firebase Cloud Functions 通过 Cloud Messaging 向 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);
Run Code Online (Sandbox Code Playgroud)
部分云功能:
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);
});
})
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
我写信给 Firebase 支持,问题出在消息类型上。在 Firebase 中,我们有两种类型的消息:数据和通知。在这种情况下,我们应该使用类型通知(参见文档)。
有效负载应如下所示:
var payload = {
notification: {
title: options.title,
body: options.text,
},
data: {
type: options.type,
}
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3636 次 |
| 最近记录: |