当特定对象的值更新时,我需要向用户触发推送通知。
例如,在待办事项应用程序中,如果用户通过时钟提醒共享任务列表,如果为某些用户更新时钟提醒的时间,则应通过推送通知通知其他所有人。
谢谢你。
干杯
当特定对象的值更新时,我需要向用户触发推送通知。
例如,在待办事项应用程序中,如果用户通过时钟提醒共享任务列表,如果为某些用户更新时钟提醒的时间,则应通过推送通知通知其他所有人。
谢谢你。
干杯
对象更新后,您可以使用 Cloud Code 触发推送。您很可能希望查看一个afterSave
挂钩以向所有相关用户发送通知。
然而,钩子有一个陷阱,它们被限制为 3 秒的挂钟时间,并且取决于您需要查询的用户数量,这可能还不够。所以我的建议是在一个特殊的表(我们称之为 NotificationQueue)中创建一个由后台作业查询的条目,后台作业最多可以运行 15 分钟。
因此,您将安排一个后台作业“轮询”此表以获取新事件以发送通知,将通知发送给用户,然后从该表中删除对象。
我的方法的一些伪代码
afterSave 挂钩
Parse.Cloud.afterSave("YourObjectClass", function(req) {
// Logic to check if you should really send out a notification
// ...
var NotificationObject = Parse.Object.extend("NotificationQueue");
var notification = new NotificationObject();
notification.set("recipients", [..array of user objects/ids..]);
notification.save(null, {
success: function(savedObject) {
// New object saved, it should be picked up by the job then
},
error: function(object, error) {
// Handle the error
}
});
});
后台作业
Parse.Cloud.job("sendNotifications", function(req,res) {
// setup the query to fetch new notification jobs
var query = new Parse.Query("NotificationQueue");
query.equalTo("sent", false);
query.find({
success: function(results) {
// Send out the notifications, see [1] and mark them as sent for example
},
error: function(error) {
// Handle error
}
});
// ...
});
[1] https://www.parse.com/docs/push_guide#sending/JavaScript
[2] https://www.parse.com/docs/cloud_code_guide#functions-aftersave
[3] https://www.parse .com/docs/cloud_code_guide#jobs