Javascript 如何使用Firebase云消息在Angular/Ionic中推送客户端到客户端的通知?
我需要做客户端推送通知(不是服务器到客户端),我想在用户向另一个用户发送消息时触发通知 可能吗?基于firebase实时数据库中的这种结构,我如何做到这一点: Firebase结构图像:Javascript 如何使用Firebase云消息在Angular/Ionic中推送客户端到客户端的通知?,javascript,angular,firebase,ionic-framework,push-notification,Javascript,Angular,Firebase,Ionic Framework,Push Notification,我需要做客户端推送通知(不是服务器到客户端),我想在用户向另一个用户发送消息时触发通知 可能吗?基于firebase实时数据库中的这种结构,我如何做到这一点: Firebase结构图像: 您可以使用firebase云函数来实现这一点。您可以添加一个云函数,当您在messages集合中添加消息时将触发该函数。然后发送通知 您可以在此处看到cloud firestore触发器: 下面是一个示例云函数,用于在用户订阅时发出通知: exports.newSubscriberNotification =
您可以使用firebase云函数来实现这一点。您可以添加一个云函数,当您在messages集合中添加消息时将触发该函数。然后发送通知 您可以在此处看到cloud firestore触发器: 下面是一个示例云函数,用于在用户订阅时发出通知:
exports.newSubscriberNotification = functions.firestore
.document('subscribers/{subscriptionId}')
.onCreate(async event => {
const data = event.after.data();
const userId = data.userId
const subscriber = data.subscriberId
// Notification content
const payload = {
notification: {
title: 'New Subscriber',
body: `${subscriber} is following your content!`,
icon: '[insert icon url]'
}
}
// ref to the device collection for the user
const db = admin.firestore()
const devicesRef = db.collection('devices').where('userId', '==', userId)
// get the user's tokens and send notifications
const devices = await devicesRef.get();
const tokens = [];
// send a notification to each device token
devices.forEach(result => {
const token = result.data().token;
tokens.push( token )
})
return admin.messaging().sendToDevice(tokens, payload)
});
您可以使用firebase云函数来实现这一点。您可以添加一个云函数,当您在messages集合中添加消息时将触发该函数。然后发送通知 您可以在此处看到cloud firestore触发器: 下面是一个示例云函数,用于在用户订阅时发出通知:
exports.newSubscriberNotification = functions.firestore
.document('subscribers/{subscriptionId}')
.onCreate(async event => {
const data = event.after.data();
const userId = data.userId
const subscriber = data.subscriberId
// Notification content
const payload = {
notification: {
title: 'New Subscriber',
body: `${subscriber} is following your content!`,
icon: '[insert icon url]'
}
}
// ref to the device collection for the user
const db = admin.firestore()
const devicesRef = db.collection('devices').where('userId', '==', userId)
// get the user's tokens and send notifications
const devices = await devicesRef.get();
const tokens = [];
// send a notification to each device token
devices.forEach(result => {
const token = result.data().token;
tokens.push( token )
})
return admin.messaging().sendToDevice(tokens, payload)
});
您是否需要有一个实际的弹出通知,或者您可以在应用程序中处理这个问题 如果您可以在应用程序中处理它,那么您可以使用firebase
addChildEventListener
来处理您正在观看的用户消息集合的更新。在事件处理程序中,您将得到一个结果。键入
,它将告诉您何时发生了ChildAdded
事件,然后您的应用程序可以向用户显示通知
您可以在Firebase文档中了解更多信息。您需要实际弹出通知,还是可以在应用程序中处理此问题 如果您可以在应用程序中处理它,那么您可以使用firebase
addChildEventListener
来处理您正在观看的用户消息集合的更新。在事件处理程序中,您将得到一个结果。键入
,它将告诉您何时发生了ChildAdded
事件,然后您的应用程序可以向用户显示通知
您可以在Firebase文档中了解更多信息。实际上,我已经编写了一个云函数,当您在messages collection()中添加消息时会触发该函数。但在您的URL中,我没有找到在向messages collection添加消息后在用户之间推送通知的方法。嗨,为了发送通知。您将使用firebase.messaging()您可以在这里看到文档。我添加了一个示例云函数,用于向订阅者发送通知。实际上,我已经编写了一个云函数,当您在messages collection()中添加消息时会触发该函数。但在您的URL中,我没有找到在向messages collection添加消息后在用户之间推送通知的方法。嗨,为了发送通知。您将使用firebase.messaging()您可以在这里看到文档,我添加了一个示例云函数,用于向subscriber.THX发送通知。不,我需要一个真正的通知,比如Whatsapp或FB Messenger.THX。不,我需要像Whatsapp或FB Messenger这样的真正通知。