Send Push Notifications with WordPress

Why send push notifications with WordPress you ask? The WP REST API will soon become part of the WordPress core, and you may want to someday integrate your WordPress site with an iOS app. To be clear, this tutorial will cover how to get selected WordPress data to the Parse.com service.

wp-parse-swift

We will not be covering how to set up push notifications in Swift. That tutorial will come at a later date.

Requirements

  • A Parse.com account
  • A self hosted WordPress site
  • PHP 5.4 or greater
  • WordPress developer level skills

Signing Up to Parse.com

This is very very easy. Visit parse.com to sign up. Upon registration, in addition to  your email and password, you will also be asked for the name of your first Parse app. I named my app “WordPress Push” for this tutorial. You can name your app anything you wish.

Download the Parse PHP SDK

In another tab, visit https://github.com/parseplatform/parse-php-sdk and download the ParsePlatform SDK. Unzip the archive in the currently used theme folder in your WordPress installation. You can create a new file to handle the Push class or you can add the class to the functions.php. For this tutorial, I’ll be using the functions.php.

At the very top of your functions.php, add the following:

after of later in the functions.php, add the following class and initializer:

So what’s going on here?

The WP_Parse_Push class will fire the _push_parse method when the core save_post method is fired. We’re using the classes (ParseClient, ParsePush) and methods (initialize, send) provided by the Parse SDK to authorize and push a newly created post title to the Parse server.

Parse Channels

In order for your your future iOS/Android apps to recognize push notifications from your WordPress installation, you must at the very least create a channel.

Channels are created at runtime, and they should not contain any spaces or special characters. The mobile devices subscribed to your channel will receive your push notification.

Per Parse:

USING CHANNELS

The simplest way to start sending notifications is using channels. This allows you to use a publisher-subscriber model for sending pushes. Devices start by subscribing to one or more channels, and notifications can later be sent to these subscribers.

Before testing, make sure you populate the $parse array with the needed parse credentials.

Your Parse Application ID, REST API Key, and Master Key can be found by navigating back the the Parse dashboard > Your WordPress Push App > Settings > Keys.

Now go ahead and make your first Push Post…

test-push-to-parse

Now navigate back to your Parse dashboard. Click your app and click the Push menu above…

parse-push-dashboard

We successfully sent a push notification to Parse via WordPress. Now that you got the Gist of this tutorial, stay tuned for the next installment where we will create a basic iOS app that will receive our WordPress pushed notifications.

And if you have any questions, suggestions or complaints, leave a comment below!