-2

I need to trigger a push notification to users when a specific object has his value updated.

For example, in a to-do app if users share a list of tasks with clock alerts, if the time of the clock alert is updated for some user, every else should be notified with push notification.

Thank you.

Cheers

2 Answers2

0

Use a local push notification service, please follow reference

Can check this tutorial as well

Satyaranjan
  • 137
  • 1
  • 9
0

You can use Cloud Code to trigger a push once an object gets updated. You most likely want to look at an afterSave hook to send out a notification to all users involved.

However there is a gotcha with the hooks, they are limited to 3 seconds of wall clock time and depending on the amount of users you need to query for this, it might not be enough. So my suggestion is to create an entry in a special table (lets call it NotificationQueue) that gets queried by a background job, background jobs can run for up to 15 minutes.

So you will schedule a background job that "polls" this table for new events to send out the notifications for, send the notifications to the users and then remove the object from this table for example.

Some pseudo code of what my approach looks like

The afterSave hook

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
        }
    });
});

The background job

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

Björn Kaiser
  • 9,882
  • 4
  • 37
  • 57