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:
[sourcecode language=”javascript”]
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()
}
[/sourcecode]
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:
[sourcecode language=”javascript”]
// 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’);
});
[/sourcecode]
This cloud function can then be called from your iOS app using the `PFCloud.callFunctionInBackground` method:
[sourcecode language=”javascript”]
let data = [
"badge" : "Increment",
"alert" : "Some message",
]
let request = [
"someKey" : someKey,
"data" : data
]
PFCloud.callFunctionInBackground("iosPush", withParameters: request as [NSObject : AnyObject])
[/sourcecode]
Leave a Reply