成功发送到设备后,Firebase 云消息传递不起作用
Firebase cloud messaging does not work after successfully sendToDevice
我使用 Firebase Cloud Functions 通过云消息传递向 iOS 设备发送通知。 sendToDevice
方法效果很好,returns 成功承诺在 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
我认为这是您的负载问题。
使用您的 firebase 函数尝试一次此负载,如果您在 iOS 收到通知,请告诉我。
var payload = {
data: {
title: "Welcome to My Group",
message: "You have new messages"
}
};
我写信给 Firebase 支持,问题出在消息类型上。在 Firebase 中,我们有两种类型的消息:数据和通知。在这种情况下,我们应该使用类型通知 (see documentation)。
有效载荷应如下所示:
var payload = {
notification: {
title: options.title,
body: options.text,
},
data: {
type: options.type,
}
};
我使用 Firebase Cloud Functions 通过云消息传递向 iOS 设备发送通知。 sendToDevice
方法效果很好,returns 成功承诺在 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
我认为这是您的负载问题。
使用您的 firebase 函数尝试一次此负载,如果您在 iOS 收到通知,请告诉我。
var payload = {
data: {
title: "Welcome to My Group",
message: "You have new messages"
}
};
我写信给 Firebase 支持,问题出在消息类型上。在 Firebase 中,我们有两种类型的消息:数据和通知。在这种情况下,我们应该使用类型通知 (see documentation)。
有效载荷应如下所示:
var payload = {
notification: {
title: options.title,
body: options.text,
},
data: {
type: options.type,
}
};