Best practices for sending bulk 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.
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 in 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 segment for the settings screen.
name : Subscription group.
label : Subscription Group label.
settings : Subscription types
$this->register_subscription_group( $name, $label,$settings = array() );
4. Once a group has been registered, add Push Notification into the created group using this method:
name : Subscription type.
label : Subscription type label.
admin_label : Subscription type label which is used for admin settings.
extras : Subscription type extra information.
$this->register_subscription_type( $name, $label, $admin_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 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.
subscription_type : Subscription type.
$this->register_push_to_normal($component_name, $component_action, $subscription_type );
Create New Push notification
6. To start sending push notifications, use this method into 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.
push_data : Notification data that is sent to mobile. Mostly this is used to pass a permalink.
Link : after clicking the Push notification, User will be directed to this link.
subscription_type : Subscription type label. 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(),
'push_data' => array(
'link' => $link,
),
$this->send_push(
array(
'primary_text' => $primary_text,
'secondary_text' => $secondary_text,
'user_ids' => $users,
'data' => array(),
'push_data' => array(
'link' => $link,
),
'subscription_type' => $subscription_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 notification directly, register a job using `BuddyBossApp\Jobs` class. This job will allow you to pass details like page or number of items or 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_bookas your job name then the filter name will 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 below 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’ll create BuddyBoss platform notification for the same.
$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 custom automatic push notification.
*/
namespace BuddyBossApp\Custom;
use BuddyBossApp\Jobs;
use BuddyBossApp\Notification\IntegrationAbstract;
class ClassName extends IntegrationAbstract {
function load() {
$this->hooks();
$this->register_subscription_group( $name, $label, $settings = array( $subscription_type ) );
$this->register_subscription_type( $subscription_type, $label, $admin_label, $extras = array() );
// $this->register_push_to_normal($component_name, $component_action, $subscription_type );
}
/**
* register hooks for sending notification.
*/
public function hooks() {
add_action( '<event_hook>', array( $this, 'send_push_notification' ), 999, 2 );
add_action( 'bbapp_queue_task_<job_name>', array( $this, 'handle_job' ), 999 );
}
public function send_push_notification() {
$jobs = Jobs::instance();
$jobs->add( '<job_name>', array( 'paged' => 1, 'timestamp' => time() ) );
$jobs->start();
}
public function handle_job( $task ) {
$task_data = maybe_unserialize( $task->data );
$user_ids = array();
// Write you logic to get users_ids
if ( ! empty( $user_ids ) ) {
$this->send_push(
array(
'primary_text' => $primary_text,
'secondary_text' => $secondary_text,
'user_ids' => $user_ids,
'data' => array(),
'push_data' => array(
'link' => $link,
),
'subscription_type' => $subscription_type,
'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>',
)
)
);
// Below job only needs to add if item remains to send.
$jobs = Jobs::instance();
$jobs->add( '<job_name>', array(
'paged' => ( $task_data['paged'] + 1 ),
'timestamp' => time(),
) );
$jobs->start();
}
}
function format_notification( $component_name, $component_action, $item_id, $secondary_item_id, $notification_id ) {
// TODO: Implement format_notification() method.
}
}
Example:
<?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.
* Withing one group you can add multiple subscription type.
*
* @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 notification manage tab for user in app.
* @param string $admin_label Subscription label for admin. This label will be visible on Push notification manage setting page for admin.
*/
$this->register_subscription_type( 'book_published', __( "A book publish.", "buddyboss-app" ), __( "A member published 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(),
'push_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,
* )
*/
}
}