1. Developer Tutorials
  2. Creating Automatic Push Notifications

Creating Automatic Push Notifications

In this tutorial, you will learn how to register your own automatic push notifications that will trigger based on the criteria you specify. Once registered, you’ll be able to enable and disable them in the Push Notifications section of the BuddyBoss App Plugin’s settings page. For more information on how push notifications work, see our Push Notifications tutorial. Make sure to also read our tutorial for displaying custom push notifications in the app.

You can optionally skip to the “Example Plugin” section at the end of the article, for an example plugin you can quickly test with.


To start, you will need to extend our abstract class by following these steps:

1. Create one class file and extend ‘BuddyBossApp\Notification\IntegrationAbstract’

use BuddyBossApp\Notification\IntegrationAbstract;

class <ClassName> extends IntegrationAbstract  {
   // Class code
}

2. Extend two abstract methods into the newly created class from Step 1.

use BuddyBossApp\Notification\IntegrationAbstract;

class <ClassName> extends IntegrationAbstract  {

   function load() {
		// TODO: Implement load() method.
	}

	function format_notification( $component_name, $component_action, $item_id, $secondary_item_id, $notification_id ) {
		// TODO: Implement format_notification() method.
	}
   
}

3. Register a group for your push notifications. This will help manage similar kinds of notifications in one section on the settings screen.

name 	 	: Group unique list.
label 	 	: Group name.
$this->register_push_group( $name, $label );

4. Once a group has been registered, add the Push Notification into the created group using this method:

name 		: Register type
admin_label 	: label which is used for admin settings.
user_label 	: label which is used for user settings.
extras		: Extra params. 
$this->register_push_type( $type, $admin_label, $user_label, $extras = array());

Extend BuddyBoss Platform custom notification as Push Notification

5. All core notifications of BuddyBoss Platform are already supported by BuddyBoss App, but if you have created custom notifications in the BuddyBoss platform and you want to extend those as push notifications on mobile devices, you can easily do so by using this method:

component_name		: Component name.
component_action 	: Component action.
push_type	: (optional) name of the push subscription.
$this->register_push_to_normal($component_name, $component_action, $push_type );

Create new Push Notification

6. To start sending push notifications, use this method in your event hook:

primary_text 		: Notification primary text.
secondary_text 		: Notification secondary text.
user_ids 		: User ids that will receive this notification.
data			: Notification extra data.
type 	: push typ. This is an important parameter as this value is mapped with the user’s/admin’s notification settings. 
$this->send_push(
    array(
        'primary_text'         	=> $primary_text,
        'secondary_text'       	=> $secondary_text,
        'user_ids'             	=> $users,
        'data'                 	=> array(),
    ),
$type
);

Send Push Notifications to a large group

Note: If you wish to send push notifications to a large group, we recommend doing it in batches for better performance.

7. In your event hook, instead of sending notifications directly, register a job using the BuddyBossApp\Jobs class. This job will allow you to pass details including page, the number of items, query argument, etc. to handle pagination.

$jobs = BuddyBossApp\Jobs::instance();
$jobs->add( 'job_name', array( 'paged' => 1, 'timestamp' => time() ) );
$jobs->start();

8. You can add extra information to your job and use those details in the job process function:

$jobs->add( 'job_name', array( 'paged' => 1, 'timestamp' => time(), 'item_id' => $item_id) );

9. Bind your job process function with job execution by using this method: 

Filter name will be bbapp_queue_task_{job name}

Example: If you registered ‘publish_book’ as your job name then the filter name would be: bbapp_queue_task_publish_book

/**
* @param $task
*/
public function handle_job_process( $task ) {

	$task_data      = maybe_unserialize( $task->data );

    // Send notification here

    // Execute below step if any item remains to send as push notification.
	$jobs = Jobs::instance();
	$jobs->add( 'publish_book', array(
		'paged'     => ( $task_data['paged'] + 1 ),
		'timestamp' => time(),
	) );
	$jobs->start();
}

add_action( 'bbapp_queue_task_{job_name}', array( $this, 'handle_job_process' ) );

10. To access job details that were passed when the job is added, use this code:

$task_data = maybe_unserialize( $task->data );

Extend created Push Notification to the web

11. To extend the created push notification to the web, pass the following data with the send_push function and it will create a BuddyBoss Platform notification as well.

$this->send_push(
	array(
            ...
            'normal_notification'      => true,
            'normal_notification_data' => array(
            'component_name'    => 'component_name',
            'component_action'  => 'component_action',
            'item_id'           => 'item_id',
            'secondary_item_id' => 'secondary_item_id',
		)

	)
);

12. To customize the format of the web notification, use format_notification extended method.

function format_notification( $component_name, $component_action, $item_id, $secondary_item_id, $notification_id ) {

		// TODO: Implement format_notification() method.

}

Template File:

<?php
/**
 * Register New custom automatic push notification.
 */
namespace BuddyBossApp\Custom;

use BuddyBossApp\Jobs;
use BuddyBossApp\Notification\IntegrationAbstract;

/**
 * Class BookPublishNotification
 *
 * @package BuddyBossApp\Custom
 */
class NewBookPublishNotification extends IntegrationAbstract {

	/**
	 *
	 */
	public function load() {

		$this->hooks();

		/**
		 * Register push group for notification.
		 * Within one group you can add multiple subscription types.
		 *
		 * @param string $name     Group unique list
		 * @param String $label    Group name.
		 *
		 */
		$this->register_push_group( 'new_book', __( "New Books", "buddyboss-app" ) );

		/**
		 * Register push type for notification.
		 *
		 * @param string $type        Register push type name
		 * @param string $admin_label Label for admin. This label will be visible on the Push notifications manage setting page for admin.
 		 * @param string $user_label  Label. This label will be visible on the Push Notifications manage tab for user in app.
		 */
		$this->register_push_type( 'new_book_published', __( "New A book publish.", "buddyboss-app" ), __( "A member publishes a new book", "buddyboss-app" ), array( 'push_group' => 'new_book' ) );
	}

	/**
	 * register hooks for sending notifications.
	 */
	public function hooks() {
		add_action( 'publish_book', array( $this, 'send_publish_book_notification' ), 999, 2 );
		add_action( 'bbapp_queue_task_publish_book', array( $this, 'handle_publish_book_job' ), 999 );
	}

	/**
	 * @param $post_id
	 * @param $post
	 */
	public function send_publish_book_notification( $post_id, $post ) {
		/**
		 * If you are sending a notification to large then you can't send notification to all users together due to PHP or server limited.
		 * Instead of sending notification to all users you need to do divided all users into the batch for better performance.
		 * you can create batch using out class \BuddyBossApp\Jobs
		 */

		$jobs = Jobs::instance();
		$jobs->add( 'publish_book', array( 'book_id' => $post_id, 'paged' => 1, 'timestamp' => time() ) );
		$jobs->start();
	}

	/**
	 * @param $task
	 */
	public function handle_publish_book_job( $task ) {

		$task_data      = maybe_unserialize( $task->data );
		$primary_text   = __( "New book published!" );
		$secondary_text = sprintf( __( "%s" ), get_the_title( $task_data['book_id'] ) );

		$users = get_users( array(
			'fields' => 'ids',
			'number' => 200,
			'paged'  => $task_data['paged'],
		) );

		if ( ! empty( $users ) ) {
			$this->send_push(
				array(
					'primary_text'             => $primary_text,
					'secondary_text'           => $secondary_text,
					'user_ids'                 => $users,
					'data'                       => array(
						'link' => get_permalink( $task_data['book_id'] ),
					),
					'subscription_type'        => 'new_book_published',
					'normal_notification'      => true,
					'normal_notification_data' => array(
						'component_name'    => 'book',
						'component_action'  => 'lesson_available',
						'item_id'           => $task_data['book_id'],
						'secondary_item_id' => get_the_title( $task_data['book_id'] ),
					)
				)
			);

			$jobs = Jobs::instance();
			$jobs->add( 'publish_book', array(
				'book_id'   => $task_data['book_id'],
				'paged'     => ( $task_data['paged'] + 1 ),
				'timestamp' => time(),
			) );
			$jobs->start();
		}
	}

	/**
	 * @param $component_name
	 * @param $component_action
	 * @param $item_id
	 * @param $secondary_item_id
	 * @param $notification_id
	 *
	 * @return array|void
	 */
	public function format_notification( $component_name, $component_action, $item_id, $secondary_item_id, $notification_id ) {
		/**
		 * This will help to update notification content for web.
		 * You need to return data in following structure
		 *  array(
		 *      'text' => Notification text/html,
		 *      'link' => link of content,
		 *  )
		 */
		if ( 'new_book_published' === $component_action ) {
			return array(
				'text' => $secondary_item_id,
				'link' => get_permalink( $item_id ),
			);
		}
	}
}

Example Plugin

You can assemble the two PHP template files below into a folder, to create a plugin that will add new automated push notification for “Books”, which can be seen at BuddyBoss > Settings > Push Notifications.

Note: For this to actually work in your app or website, we are assuming you have also already registered a “book” custom post type in WordPress. Alternatively, you can adjust this code to use your own custom post type.

buddyboss-app-custom.php

This is the loader file. Create a plugin folder and add this file into it.

<?php
/*
Plugin Name: BuddyBoss App Custom Automated Push Notification Example
Plugin URI: {add your own}
Description: {add your own}
Version: 1.0.0
Author: {add your own}
Author URI: {add your own}
*/
if ( ! defined( 'ABSPATH' ) ) {
	exit();
}

/**
 * Load Custom Automated Push Notification
 */
function bbapp_custom_work_init() {
	if ( class_exists( 'bbapp' ) ) {
		include 'buddyboss-app-custom-push-notification.php';
		BuddyBossApp\Custom\BookPublishNotification::instance();
	}
}
add_action( 'plugins_loaded', 'bbapp_custom_work_init' );

buddyboss-app-custom-push-notification.php

This file also goes into your plugin folder. This contains most of the functional code to register the new automated push notification.

<?php
/**
 * Register custom automatic push notification.
 */

namespace BuddyBossApp\Custom;

use BuddyBossApp\Jobs;
use BuddyBossApp\Notification\IntegrationAbstract;

/**
 * Class BookPublishNotification
 * @package BuddyBossApp\Custom
 */
class BookPublishNotification extends IntegrationAbstract {

	/**
	 *
	 */
	public function load() {

		$this->hooks();

		/**
		 * Register subscription group for notification.
		 * Within one group you can add multiple subscription types.
		 *
		 * @param string $name     Group unique list
		 * @param String $label    Group name.
		 * @param array  $settings subscription type list which we want to bind with this group.
		 *
		 */
		$this->register_subscription_group( 'book', __( "Books", "buddyboss-app" ), array(
			'book_published',
		) );

		/**
		 * Register subscription type for notification.
		 *
		 * @param string $name        Subscription type name
		 * @param string $label       Subscription label. This label will be visible on Push Notifications manage tab for user in app.
		 * @param string $admin_label Subscription label for admin. This label will be visible on Push notifications manage setting page for admin.
		 */
		$this->register_subscription_type( 'book_published', __( "A book publish.", "buddyboss-app" ), __( "A member publishes a new book", "buddyboss-app" ) );
	}

	/**
	 * register hooks for sending notification.
	 */
	public function hooks() {
		add_action( 'publish_book', array( $this, 'send_publish_book_notification' ), 999, 2 );
		add_action( 'bbapp_queue_task_publish_book', array( $this, 'handle_publish_book_job' ), 999 );
	}

	/**
	 * @param $post_id
	 * @param $post
	 */
	public function send_publish_book_notification( $post_id, $post ) {
		/**
		 * If you sending a notification to large then you can't send notification to all users together due to PHP or server limited.
		 * Instead of sending notification to all user you need to do divided all users into the batch for better performance.
		 * you can create batch using out class \BuddyBossApp\Jobs
		 */

		$jobs = Jobs::instance();
		$jobs->add( 'publish_book', array( 'book_id' => $post_id, 'paged' => 1, 'timestamp' => time() ) );
		$jobs->start();
	}

	/**
	 * @param $task
	 */
	public function handle_publish_book_job( $task ) {

		$task_data      = maybe_unserialize( $task->data );
		$primary_text   = __( "New book published!" );
		$secondary_text = sprintf( __( "%s" ), get_the_title( $task_data['book_id'] ) );

		$users = get_users( array(
			'fields' => 'ids',
			'number' => 200,
			'paged'  => $task_data['paged'],
		) );

		if ( ! empty( $users ) ) {
			$this->send_push(
				array(
					'primary_text'             => $primary_text,
					'secondary_text'           => $secondary_text,
					'user_ids'                 => $users,
					'data'                        => array(
					    'link' => get_permalink( $task_data['book_id'] ),
					),
					'subscription_type'        => 'book_published',
					'normal_notification'      => true,
					'normal_notification_data' => array(
						'component_name'    => 'book',
						'component_action'  => 'lesson_available',
						'item_id'           => $task_data['book_id'],
						'secondary_item_id' => get_the_title( $task_data['book_id'] ),
					)
				)
			);

			$jobs = Jobs::instance();
			$jobs->add( 'publish_book', array(
				'book_id'   => $task_data['book_id'],
				'paged'     => ( $task_data['paged'] + 1 ),
				'timestamp' => time(),
			) );
			$jobs->start();
		}
	}


	/**
	 * @param $component_name
	 * @param $component_action
	 * @param $item_id
	 * @param $secondary_item_id
	 * @param $notification_id
	 *
	 * @return array|void
	 */
	public function format_notification( $component_name, $component_action, $item_id, $secondary_item_id, $notification_id ) {
		/**
		 * This will help to update notification content for web.
		 * You need to return data in following structure
		 *  array(
		 *      'text' => Notification text/html,
		 *      'link' => link of content,
		 *  )
		 */
	}
}

Questions?

We're always happy to help with questions you might have! Search our documentation, contact support, or connect with our sales team.