我正在使用NPM Firebase管理员模块从传统HTTP迁移到v1。但是我有一个问题,当发送通知时,它不会给出任何错误,但它不会返回消息ID,并且设备上没有收到通知。
服务器代码:
const admin = require('firebase-admin');
var serviceAccount = require('./file.json');
admin.initializeApp({
credential: admin.credential.cert(serviceAccount)
});
const messaging = admin.messaging();
async function sendPush() {
const message2 = {
notification: {
title: 'test title',
body: 'test'
},
token: 'valid-token'
};
try {
const test = await messaging.send(message2);
console.log('--------------------------Successfully sent message:--------------------------');
console.log(test);
console.log('------------------------------------------------------------------------------');
} catch(err) {
// Will catch both thrown exceptions as well as rejections
console.log('--------------------------Error sending message:', err);
}
}
当我使用sendPush功能发送通知时,我在控制台中收到以下响应:
--------------------------Successfully sent message:--------------------------
projects/project-name/messages/
------------------------------------------------------------------------------
它不包含文档中指定的消息ID,我没有收到通知。
文档样本响应:
projects/myproject-b5ae1/messages/0:1500415314455276%31bd1c9631bd1c96
编辑:
我测试了sendToDevice函数(Legacy API)从Firebase管理员和它的工作。
messaging.sendToDevice('valid-token', message)
.then(response => {
console.log('--------------------------Successfully sent message:--------------------------');
console.log(response);
console.log('------------------------------------------------------------------------------');
})
.catch(err => {
console.log('--------------------------Error sending message:', err);
});
推送被交付。但是发送,sendMulticast和sendAll(v1 API)仍然不能工作。
解决了的。
问题是来自android的令牌对于新FCM版本不正确。在应用程序中更新令牌生成后,它已开始与legacy和v1一起工作。