Categories
Past Tutorials WordPress

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.

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:

require('parse-php-sdk-master/autoload.php');

use Parse\ParseObject;
use Parse\ParseQuery;
use Parse\ParseACL;
use Parse\ParsePush;
use Parse\ParseUser;
use Parse\ParseInstallation;
use Parse\ParseException;
use Parse\ParseAnalytics;
use Parse\ParseFile;
use Parse\ParseCloud;
use Parse\ParseClient;

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

class WP_Parse_Push
{
	public function __construct(){
		add_action( 'save_post', array( $this, '_push_parse'), 15 );
	}
	
	function _push_parse( $post_id ) 
	{
		if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
			return;
		}
		
		if ( ! current_user_can( 'edit_post', $post_id ) ) {
			return;
		}
		
		$pushed = get_post_meta($post_id,'_parse_push', true);
		
		$parse = array(
			'application_id' => '', //Parse Application ID
			'rest_api_key' => '', //Parse REST API Key
			'master_key' => '', // Parse Master Key
		);
		
		ParseClient::initialize( 
			esc_attr($parse['application_id']), 
			esc_attr($parse['rest_api_key']), 
			esc_attr($parse['master_key'])
		);
		
		//Only Push One Time
		if( $_POST['post_status'] == 'publish' && empty($pushed) )
		{
			$data = array(
				"alert" => sanitize_text_field($_POST['post_title']),
				//"badge" => "Increment",
			);

			ParsePush::send(array(
			    "channels" => [News],
			    "data" => $data
			));
			
			update_post_meta($post_id,'_parse_push', 1);
		}
	}
}

if( is_admin() ){
	$push = new WP_Parse_Push;
}

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.

			ParsePush::send(array(
			    "channels" => [News],
			    "data" => $data
			));

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.

		$parse = array(
			'application_id' => '', //Parse Application ID
			'rest_api_key' => '', //Parse REST API Key
			'master_key' => '', // Parse Master Key
		);

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…

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

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!