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])
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 🙂
It’s been a while since I did this, and it’s a bit hard to get my head back into it, but yes, I think when you set up your cloud application on Heroku, you should set it up locally as well to configure the cloud/main.js file. If you didn’t do that, I believe this link should help with that: https://github.com/ParsePlatform/parse-server-example#for-local-development
Once you have it locally you should be able to find the cloud/main.js file without a problem.
Good luck with it, sympathies with you – it’s a frustrating process!
Thank you, it helped me alot 🙂
i want to send push notification based on email id of user, please could you help me out.
//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”)
thanks for reply, i got in console”” PUSH OK “”but didn’t receive any notification on y iOS device
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?
[…] Sending push notifications using Parse Server […]
[…] 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 […]
How can I add an action to these notifications like “Accept” and “Decline” on a friend request?
Hi Michael – take a look at “Assigning Custom Actions to a Remote Notification” in Apple docs, hopefully that will help: https://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/CreatingtheNotificationPayload.html#//apple_ref/doc/uid/TP40008194-CH10-SW4