作者:94爱拍就是爱拍 | 来源:互联网 | 2023-05-18 09:00
IamtryingtosendnotificationfromNode.jsservertoaniosapplication.ItseemsworkingifIse
I am trying to send notification from Node.js server to an ios application. It seems working if I send notification from Firebase console, but isn't working if try from my node.js server using firebase-admin sdk.
我正在尝试从Node.js服务器向ios应用程序发送通知。如果我从Firebase控制台发送通知似乎有效,但如果从我的node.js服务器使用firebase-admin sdk尝试,则无法正常工作。
I followed tutorial from https://firebase.google.com/docs/cloud-messaging/admin/send-messages.
我按照https://firebase.google.com/docs/cloud-messaging/admin/send-messages上的教程进行了操作。
One thing I do not understand is the response after sending notification seems working. I get below response.
我不明白的一件事是发送通知后的响应似乎有效。我得到以下回应。
{
"results": [
{
"messageId": "0:1511109840587284%a63b4c28f9fd7ecd"
}
],
"canonicalRegistrationTokenCount": 0,
"failureCount": 0,
"successCount": 1,
"multicastId": 7436388871122493000
}
Does anyone know what I am doing wrong?
有谁知道我做错了什么?
-- Edit
- 编辑
Here is the code that sends the notification. admin is the firebase-admin instance.
以下是发送通知的代码。 admin是firebase-admin实例。
router.post('/notify', (req, res) => {
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: ".firebaseio.com"
});
var registratiOnTokens= [
'tokenFromIosApp'
];
var payload = {
data : {
body : 'TEST'
}
};
admin.messaging().sendToDevice(registrationTokens, payload)
.then((response) => {
console.log('Sent successfully.\n');
console.log(response);
res.status(statusCodes.Ok);
res.json(response);
})
.catch((error) => {
console.log('Sent failed.\n');
console.log(error);
res.status(statusCodes.InternalServerError);
res.json(error);
});
});
1 个解决方案