Sending push notifications using Parse Server

Finally – phew!

I have migrated Grabble Words from Parse to using Parse server using herokuapp (a platform as a service (PaaS) which you can use to run applications in the cloud.) with the backend hosted by mongodb (database hosting service). Definitely was not a straight-forward migration, especially for someone more comfortable working on front-end applications in nice GUI’s rather than back-end in the terminal.

I came across an issue with very little documentation/tutorials that I thought worth a little write-up: Sending push notifications using Parse Server from your app.

Previously the notification was set up using the PFPush object, something like:

if let pushQuery = PFInstallation.query() {

    pushQuery.whereKey("someKey", equalTo: someKey)
    let data = [
        "badge" : "Increment",
        "alert" : "Some message",
    ]
    // Send push notification to query
    let push = PFPush()
    push.setQuery(pushQuery) // Set our Installation query
    push.setData(data)

    push.sendPushInBackground()
}

PFPush is unfortunately disabled from the client(i.e. your app) now due to security issues. PFPush is, however, still available on server side code, which can be called from your app.

Unfortunately tutorials and guides on notifications using Parse Server (such as this) only go as far as triggering a notification from curl or cloud code, and don’t go into how the app can request the notification, other than to ironically state that ‘the process is fairly straightforward’…

Well, it is straightforward, when you know how to do it! (Follow the tutorial at codepath up until it dispatches notifications using curl or cloud code and then come back here to continue setting up the notification from your app)

When you set up your cloud application on Heroku, you have the option to set up Parse cloud code, which is basically javascript functions defined on the Parse.Cloud object. These are defined in the cloud/main.js file. I set up a function in this file that accepts data and pushes this data to a notification using Parse.Push:

// iOS push testing
Parse.Cloud.define("iosPush", function(request, response) {

  var user = request.user;
  var params = request.params;
  var someKey = params.someKey
  var data = params.data

  var pushQuery = new Parse.Query(Parse.Installation);
  pushQuery.equalTo('deviceType', 'ios'); // targeting iOS devices only
  pushQuery.equalTo("someKey", someKey)

  Parse.Push.send({
    where: pushQuery, // Set our Installation query
    data: data
  }, { success: function() {
      console.log("#### PUSH OK");
  }, error: function(error) {
      console.log("#### PUSH ERROR" + error.message);
  }, useMasterKey: true});

  response.success('success');
});

This cloud function can then be called from your iOS app using the `PFCloud.callFunctionInBackground` method:

let data = [
    "badge" : "Increment",
    "alert" : "Some message",
]
let request = [
    "someKey" : someKey,
    "data" : data
]
PFCloud.callFunctionInBackground("iosPush", withParameters: request as [NSObject : AnyObject])

iOS development with Swift - book: https://manning.com/books/ios-development-with-swift video course: https://www.manning.com/livevideo/ios-development-with-swift-lv

Tagged with: ,
Posted in Swift
12 comments on “Sending push notifications using Parse Server
  1. Lend Kazazi says:

    I really need your help in this. I came across the same issue that you have. I was able to send push notifications previously on parse but now that i migrated to Heroku, things got a bit confusing. My question is where should i add the Parse Cloud code and where is the cloud/main.js file located. Thank you 🙂

  2. Lend Kazazi says:

    Thank you, it helped me alot 🙂

  3. i want to send push notification based on email id of user, please could you help me out.

  4. //calling from Xcode
    [PFCloud callFunctionInBackground:@”iosPush”
    withParameters:@{@”email”:@”xxxx@xxxx.com”}
    block:^(id object, NSError *error) {
    if (!error) {

    NSLog(@”object:%@”,object);
    // ratings is 4.5
    }
    else{
    NSLog(@”error after call push:%@”,error);
    }
    }];

    //my cloud code
    Parse.Cloud.define(“iosPush”, function(request, response) {

    var user = request.user;
    var params = request.params;
    var email = params.email
    // var data = params.data
    var payload = {
    alert: “testing push for device”,
    sound: ‘default’,
    badge: 1

    // you can add other stuff here…
    };

    var pushQuery = new Parse.Query(Parse.Installation);
    pushQuery.equalTo(‘deviceType’, ‘ios’); // targeting iOS devices only
    pushQuery.equalTo(“email”, email)

    Parse.Push.send({
    where: pushQuery, // Set our Installation query
    data: payload
    }, { success: function() {
    console.log(“#### PUSH OK”);
    }, error: function(error) {
    console.log(“#### PUSH ERROR” + error.message);
    }, useMasterKey: true});

    response.success(‘success’);
    });

    2017-05-04T14:32:37.561222+00:00 app[web.1]: [32minfo[39m: Ran cloud function iosPush for user H3c5NvS5DV with:
    2017-05-04T14:32:37.561232+00:00 app[web.1]: Input: {“email”:”xxxx@xxx.com”}
    2017-05-04T14:32:37.561233+00:00 app[web.1]: Result: “success” functionName=iosPush, email”:”xxxx@xxx.com, user=xxxxx
    2017-05-04T14:32:37.777048+00:00 heroku[router]: at=info method=POST path=”/parse/push” host=appname.herokuapp.com request_id=aacb5cbc-f110-409a-b721-c1a8e2978df6 fwd=”54.158.74.201″ dyno=web.1 connect=2ms service=57ms status=200 bytes=533 protocol=https

    but i didn’t receive any notification on my device

    • My sympathies, this stuff is hard! Can you see output in the Parse console? (either “PUSH OK” or “PUSH ERROR”)

    • Hard to know exactly what the problem is without going through it step by step. Have you added push notification capabilities and registered user notification settings and registered for remote notifications in your AppDelegate?

  5. […] via herokuapp (for running applications in the cloud) and mongodb (database hosting service). Here‘s where I delved into some difficulties with push notifications at the […]

  6. How can I add an action to these notifications like “Accept” and “Decline” on a friend request?

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: