Android 使用Firebase的云函数发送推送通知
我正在尝试创建一个云函数,向给定用户发送推送通知 用户进行一些更改,并在firebase数据库中的节点下添加/更新数据(该节点表示用户id)。这里我想触发一个函数,它向用户发送推送通知 对于DB中的用户,我有以下结构Android 使用Firebase的云函数发送推送通知,android,firebase,firebase-cloud-messaging,google-cloud-functions,Android,Firebase,Firebase Cloud Messaging,Google Cloud Functions,我正在尝试创建一个云函数,向给定用户发送推送通知 用户进行一些更改,并在firebase数据库中的节点下添加/更新数据(该节点表示用户id)。这里我想触发一个函数,它向用户发送推送通知 对于DB中的用户,我有以下结构 Users - UID - - email - - token - UID - - email - - token 到目前为止,我有这个功能: exports.sendNewTripNotification = functions.database.ref('/{
Users
- UID
- - email
- - token
- UID
- - email
- - token
到目前为止,我有这个功能:
exports.sendNewTripNotification = functions.database.ref('/{uid}/shared_trips/').onWrite(event=>{
const uuid = event.params.uid;
console.log('User to send notification', uuid);
var ref = admin.database().ref('Users/{uuid}');
ref.on("value", function(snapshot){
console.log("Val = " + snapshot.val());
},
function (errorObject) {
console.log("The read failed: " + errorObject.code);
});
当我得到回调时,snapshot.val()返回null。你知道怎么解决这个问题吗?以及以后如何发送推送通知?返回此函数调用
return ref.on("value", function(snapshot){
console.log("Val = " + snapshot.val());
},
function (errorObject) {
console.log("The read failed: " + errorObject.code);
});
这将使云功能保持活动状态,直到请求完成。从Doug在评论中给出的链接了解更多关于回复承诺的信息。我成功地做到了这一点。下面是使用云函数发送通知的代码,这些函数对我有用
exports.sendNewTripNotification = functions.database.ref('/{uid}/shared_trips/').onWrite(event=>{
const uuid = event.params.uid;
console.log('User to send notification', uuid);
var ref = admin.database().ref(`Users/${uuid}/token`);
return ref.once("value", function(snapshot){
const payload = {
notification: {
title: 'You have been invited to a trip.',
body: 'Tap here to check it out!'
}
};
admin.messaging().sendToDevice(snapshot.val(), payload)
}, function (errorObject) {
console.log("The read failed: " + errorObject.code);
});
})
只是回答Jerin A Mathews的问题。。。 使用以下主题发送消息:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
//Now we're going to create a function that listens to when a 'Notifications' node changes and send a notificcation
//to all devices subscribed to a topic
exports.sendNotification = functions.database.ref("Notifications/{uid}")
.onWrite(event => {
//This will be the notification model that we push to firebase
var request = event.data.val();
var payload = {
data:{
username: request.username,
imageUrl: request.imageUrl,
email: request.email,
uid: request.uid,
text: request.text
}
};
//The topic variable can be anything from a username, to a uid
//I find this approach much better than using the refresh token
//as you can subscribe to someone's phone number, username, or some other unique identifier
//to communicate between
//Now let's move onto the code, but before that, let's push this to firebase
admin.messaging().sendToTopic(request.topic, payload)
.then((response) => {
console.log("Successfully sent message: ", response);
return true;
})
.catch((error) => {
console.log("Error sending message: ", error);
return false;
})
})
//And this is it for building notifications to multiple devices from or to one.
对于向主题发送通知,上面的代码对我很有效,如果您有任何疑问,请告诉我。在云功能中发送主题通知 主题a基本上是组您可以为所选组发送通知
var topic = 'NOTIFICATION_TOPIC';
const payload = {
notification: {
title: 'Send through Topic',
body: 'Tap here to check it out!'
}
};
admin.messaging().sendToTopic(topic,payload);
您可以从移动端为任何新的或现有的主题注册设备uuid的console.log是否显示了正确的值?是的,uuid是正确的。使用反勾号替换ref:
admin.database().ref(`Users/${uuid}`)中的uuid
的值。您还应该使用once()
而不是on()
on()
使侦听器保持连接状态;在云函数中不是你想要的东西。而且,你的函数应该返回一个承诺,当你的工作完成并且清理是安全的时,向云函数指示。如果您在不返回承诺的情况下执行异步工作,它将无法按您希望的方式工作。浏览这些视频教程以了解更多信息可能是一个好主意:Doug是正确的-对于云函数,您应该返回一个承诺,以便它可以观看链的执行,然后在完成后停止实例。因为它无法知道在这种情况下正在发生异步工作,所以函数在异步工作实际完成之前就完成了。谢谢大家的回答。把它们结合起来帮助我实现了我想要的!这是我的荣幸,如果答案对你的问题有帮助,请接受@TudorLozbais是否有任何方法可以使用firebase云功能向主题发送消息?此代码是否会向所有用户发送通知?如果我只想向一个用户发送通知,比如whatsapp消息通知,该怎么办。@DeepakGautam这将只发送到一个特定的设备。注意sendToDevice是一个遗留方法。看,我正在使用上面的云函数发送通知,但我不知道如何接收iOS。有人能告诉我如何接收上述通知并向用户显示其内容吗?您好,如果我想发送一个特定的主题怎么办?比如说,一些人订阅了《早安主题》和另一组《NoonTopic》。请帮忙。@Gsilveira你能检查一下吗,
var topic = 'NOTIFICATION_TOPIC';
const payload = {
notification: {
title: 'Send through Topic',
body: 'Tap here to check it out!'
}
};
admin.messaging().sendToTopic(topic,payload);