无法使用 Cloud Functions 发送 FCM 消息
Unable to send FCM messages using Cloud Functions
我们正在开发一个聊天应用程序,并使用云功能向使用 FCM 注册令牌的设备发送数据通知。下面是代码:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.createMessage = functions.firestore
.document('messages/{messageId}')
.onCreate(event => {
const db = admin.firestore();
var newValue = event.data.data();
var recipient = newValue.recipient_id;
var msg = newValue.message;
var sender = newValue.sender_id;
var senderRef = db.collection('users').doc(sender);
var recipientRef = db.collection('users').doc(recipient);
const senderObj = new Promise(
function(resolve, reject) {
senderRef.get()
.then(doc => {
if (!doc.exists) {
console.log('No such User document!');
reject(new Error('No such document!'));
} else {
console.log('Document data:', doc.data());
resolve(doc.data());
}
})
.catch(err => {
console.log('Error getting document', err);
reject(err);
});
});
const recipientObj = new Promise(
function(resolve, reject) {
recipientRef.get()
.then(doc => {
if (!doc.exists) {
console.log('No such User document!');
reject(new Error('No such document!'));
} else {
console.log('Document data:', doc.data());
resolve(doc.data());
}
})
.catch(err => {
console.log('Error getting document', err);
reject(err);
});
});
return Promise.all([senderObj, recipientObj]).then(results => {
var senderDetails = results[0];
var recipientDetails = results[1];
var payload = {
"data": {
"title": String(senderDetails.first_name),
"body": String(msg),
"recipientId": String(recipient),
"senderId": String(sender),
"senderImage": String(senderDetails.avatar),
"createdTime": String(newValue.created_time),
"chatType": "one",
"messageId": String(event.params.messageId)
}
};
var options = {
priority: "high"
};
/*
if (recipientDetails.device_type != "android"){
payload["content_available"] = true;
payload["priority"] = "high";
}
*/
admin.messaging().sendToDevice(recipientDetails.device_token, payload, options)
.then(function(response) {
console.log("Message sent: ", response);
})
.catch(function(error) {
console.log("Error sending message: ", error, payload);
});
});
});
这正在开发中,并且在上周之前一直在正常工作,向设备发送通知。它首先开始将消息延迟 5-10 分钟,现在我们根本收不到任何通知。云函数的日志显示消息已成功传递:(
Firestore/Cloud Functions 最近有什么变化吗?代码有什么问题吗?任何指针将不胜感激。
您需要 return 来自 sendToDevice()
的 Promise
:
return admin.messaging().sendToDevice(...) // ADDED return
我们正在开发一个聊天应用程序,并使用云功能向使用 FCM 注册令牌的设备发送数据通知。下面是代码:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.createMessage = functions.firestore
.document('messages/{messageId}')
.onCreate(event => {
const db = admin.firestore();
var newValue = event.data.data();
var recipient = newValue.recipient_id;
var msg = newValue.message;
var sender = newValue.sender_id;
var senderRef = db.collection('users').doc(sender);
var recipientRef = db.collection('users').doc(recipient);
const senderObj = new Promise(
function(resolve, reject) {
senderRef.get()
.then(doc => {
if (!doc.exists) {
console.log('No such User document!');
reject(new Error('No such document!'));
} else {
console.log('Document data:', doc.data());
resolve(doc.data());
}
})
.catch(err => {
console.log('Error getting document', err);
reject(err);
});
});
const recipientObj = new Promise(
function(resolve, reject) {
recipientRef.get()
.then(doc => {
if (!doc.exists) {
console.log('No such User document!');
reject(new Error('No such document!'));
} else {
console.log('Document data:', doc.data());
resolve(doc.data());
}
})
.catch(err => {
console.log('Error getting document', err);
reject(err);
});
});
return Promise.all([senderObj, recipientObj]).then(results => {
var senderDetails = results[0];
var recipientDetails = results[1];
var payload = {
"data": {
"title": String(senderDetails.first_name),
"body": String(msg),
"recipientId": String(recipient),
"senderId": String(sender),
"senderImage": String(senderDetails.avatar),
"createdTime": String(newValue.created_time),
"chatType": "one",
"messageId": String(event.params.messageId)
}
};
var options = {
priority: "high"
};
/*
if (recipientDetails.device_type != "android"){
payload["content_available"] = true;
payload["priority"] = "high";
}
*/
admin.messaging().sendToDevice(recipientDetails.device_token, payload, options)
.then(function(response) {
console.log("Message sent: ", response);
})
.catch(function(error) {
console.log("Error sending message: ", error, payload);
});
});
});
这正在开发中,并且在上周之前一直在正常工作,向设备发送通知。它首先开始将消息延迟 5-10 分钟,现在我们根本收不到任何通知。云函数的日志显示消息已成功传递:(
Firestore/Cloud Functions 最近有什么变化吗?代码有什么问题吗?任何指针将不胜感激。
您需要 return 来自 sendToDevice()
的 Promise
:
return admin.messaging().sendToDevice(...) // ADDED return