Parse.com - setting up push notifications for single users

297 views Asked by At

I'm working on a hybrid app using Ionic framework with Parse as a back end and I'm struggling to set up push notifications for single users. I'm sending the notifications through Parse's Cloud Code, and I have sending push notifications to all users working okay using the following;

Parse.Cloud.afterSave(
    '_User',
    function(request, response) {
        Parse.Cloud.useMasterKey();
        var installationQuery = new Parse.Query(Parse.Installation);

        Parse.Push.send(
            {
                where: installationQuery,
                data: {
                    alert: 'Test notification'
                }
            }, {
                success: function () {
                    // Push was successful
                },
                error: function (error) {
                    // Push failed
                }
            }
        );
    }
);

Currently, when a user registers an entry is made into the Installation class (using phonegap-parse-plugin), adding their user ID as a channel. I did this with the intention of adding the channel as a query constraint when determining which users to push notifications to, but adding query.contains('channels', 'user-' + request.user.id); to the above doesn't push any notifications (but reports success in Cloud Code).

I've also read about adding the user as a pointer to the installation class, but that would mean a device could only have one user.

What is the best way to allow me to send push notifications to a single user via Cloud Code? Thank you!

1

There are 1 answers

1
kingspeech On BEST ANSWER

Can you see the results of the Installation query where you add the constraint;

query.contains('channels', 'user-' + request.user.id);

The problem might be the empty result. Check it. One of suggestion to singe push single target is just use the user id. Save the user id in installation table and send push notification based on query constraint where equal to. Hope this helps.

Regards.