在我的应用程序中,当用户创建新帖子时,我会向该用户的关注者发送推送通知。正如您在下面的代码中看到的,我有一些额外的设置,我需要从每个关注者的个人资料中查询这些设置,以获取他们的推送 token 并检查一些额外的通知设置。如果用户拥有大量关注者(即 1000 个),我担心对每个用户个人资料的查询可能会成为瓶颈。
解决这个问题的最佳方法是什么?
// The cloud function to trigger when a post is created
exports.newPost = functions.database.ref('/posts/{postId}').onCreate(event => {
const postId = event.params.postId;
const post = event.data.val();
const userId = post.author;
let tokens = [];
let promises = [];
return admin.database().ref(`/followers/${userId}`).once('value', (followers) => {
followers.forEach((f) => {
let follower = f.key;
promises.push(
admin.database().ref(`users/${follower}`).once('value')
);
});
})
.then(() => {
return Promise.all(promises).then((users) => {
users.forEach((user) => {
const userDetails = user.val();
if (userDetails.post_notifications) {
if(userDetails.push_id != null) {
tokens.push(userDetails.push_id);
}
}
})
})
})
.then(() => {
if (tokens.length > 0) {
const payload = {
notification: {
title: 'New Post!',
body: 'A new post has been created'
}
};
// Send notifications to all tokens.
return admin.messaging().sendToDevice(tokens, payload);
}
});
})
编辑:
我们考虑过使用主题。但我们不确定如何让我们的自定义通知设置与主题一起使用。这就是我们的困境。
我们有多种操作可以创建通知,并且我们为应用中的每种类型的通知提供单独的开关,以便用户可以选择他们想要关闭的通知类型。
假设当用户 A 关注用户 B 时。我们可以将用户 A 订阅“用户 B 的主题”,这样每当用户 B 执行向他/她的关注者发送通知的操作时,我就可以向订阅的用户发送通知“用户B主题”。
因为我们在应用程序中有多个通知开关,并且当用户 A 更改他/她的设置时,他们不希望收到新帖子的通知,但仍希望收到他/她关注的用户的其他类型的通知,因此我们无法弄清楚我们如何在这种情况下使用主题。
最佳答案
您可以使用主题来代替使用 token 。假设用户开始关注某人,然后他将注册该主题。
假设他跟踪了一个叫“Peter”的人,那么你可以执行以下命令:
FirebaseMessaging.getInstance().subscribeToTopic("Peter");
现在,如果您有这个数据库:
posts
postid
postdetails: detailshere
author: Peter
然后使用onCreate()
:
exports.newPost = functions.database.ref('/posts/{postId}').onCreate(event => {
const postId = event.params.postId;
const post = event.data.val();
const authorname = post.author;
const details=post.postdetails;
const payload = {
data: {
title:userId,
body: details,
sound: "default"
},
};
const options = {
priority: "high",
timeToLive: 60 * 60 * 24
};
return admin.messaging().sendToTopic(authorname, payload, options);
});
您可以使用此功能,每次作者创建新帖子时,都会触发 onCreate()
然后您可以在通知中添加帖子的详细信息和作者姓名(如果您愿意)并sendToTopic()
会将其发送给订阅该主题的所有用户,即 authorname
(例如:Peter)
编辑后,我认为您希望用户取消订阅某个主题,但继续订阅其他主题,那么您必须为此使用管理 SDK:
https://firebase.google.com/docs/cloud-messaging/admin/manage-topic-subscriptions
使用管理 SDK,您还可以取消订阅某个主题的用户,一个简单的示例:
// These registration tokens come from the client FCM SDKs.
var registrationTokens = [
'YOUR_REGISTRATION_TOKEN_1',
// ...
'YOUR_REGISTRATION_TOKEN_n'
];
// Unsubscribe the devices corresponding to the registration tokens from
// the topic.
admin.messaging().unsubscribeFromTopic(registrationTokens, topic)
.then(function(response) {
// See the MessagingTopicManagementResponse reference documentation
// for the contents of response.
console.log('Successfully unsubscribed from topic:', response);
})
.catch(function(error) {
console.log('Error unsubscribing from topic:', error);
});
关于javascript - 如何针对此用例扩展 Firebase Cloud 功能中的推送通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49465512/