【发布时间】:2017-12-07 16:57:17
【问题描述】:
在一个安卓应用程序中,我使用 FCM 发送通知,云功能执行成功,如 firebase 控制台日志所示,但在我的设备中它没有显示任何通知,可能是什么原因?
下面是我的 index.js 的代码
let functions = require('firebase-functions');
let admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.sendNotification = functions.database.ref('/notifications/messages/{pushId}')
.onWrite(event => {
console.log('notifying start1');
const message = event.data.current.val();
const senderUid = message.from;
const receiverUid = message.to;
console.log('SenderId '+senderUid + ' Receiver Id '+receiverUid);
const promises = [];
console.log('notifying start2');
if (senderUid == receiverUid) {
//if sender is receiver, don't send notification
promises.push(event.data.current.ref.remove());
return Promise.all(promises);
}
console.log('notifying start3');
const getInstanceIdPromise = admin.database().ref(`/users/${receiverUid}/accessToken`).once('value');
console.log('notifying start4');
const getReceiverUidPromise = admin.auth().getUser(receiverUid);
console.log('notifying start5');
return Promise.all([getInstanceIdPromise, getReceiverUidPromise]).then(results => {
const accessToken = results[0].val();
const receiver = results[1];
console.log('notifying ' + receiverUid + ' about ' + message.body + ' from ' + senderUid);
const payload = {
notification: {
title: 'Firebase Notification',
body: message.body,
}
};
admin.messaging().sendToDevice(accessToken, payload)
.then(function (response) {
console.log("Successfully sent message:", response);
})
.catch(function (error) {
console.log("Error sending message:", error);
});
});
});
请帮忙!提前致谢。
【问题讨论】:
-
您是否从 fire-base 控制台发送了消息。
-
是的,来自 firebase 控制台它的工作。
-
您在仪表板的 Cloud Functions 日志中收到什么消息?
-
这是我得到的响应结果:[ { error: [Object] } ], canonicalRegistrationTokenCount: 0, failureCount: 1, successCount: 0, multicastId: 5973854734577752000 } 函数执行耗时 1497 ms,完成状态:'ok'
-
您确定要发送到相应的令牌吗?您能否也将您的代码发布为
onMessageReceived()
标签: android firebase firebase-cloud-messaging google-cloud-functions