It's possible to send custom push notifications using pluggable functions in AppPush.

You can write your own plugin that sends a notification based on an event in WordPress, for example, when a comment is posted. You can send notifications based on any event, you can even send notifications only to certain users.

For a developer-focused tutorial on how to customize push notifications, please  click here for our blog post.

Custom Push Notifications

Available is the apppush_send_notification() function. This will send a push to all devices.

To use, add to a custom plugin, and modify to use a WordPress hook of your choosing.

function send_custom_push_notification( $data ) {      
apppush_send_notification( $data );  
} 
add_action( 'hook', 'send_custom_push_notification');

Here's an example of sending a notification when a comment is posted:

function send_custom_push_notification( $id, $comment ) {      
$data = $comment->comment_content;      
if( function_exists('apppush_send_notification') ) {         
apppush_send_notification( $data );     
}  
} 
add_action( 'wp_insert_comment', 'send_custom_push_notification', 10, 2 );

Send pushes to specific devices

It's also possible to send notifications only to devices you choose. This is useful for users sending private messages to each other, for example in BuddyPress, or in a messaging app.

Example:

// Change this hook add_action( 'save_post', 'push_to_devices' );  
function push_to_devices() {      
// @TODO be sure to add a nonce and other type of checks here for the save_post hook or you'll get too many messages sent to the same device      
if( !class_exists( 'AppPresser_Notifications' ) )        
return;  
    
// this should be an array of user IDs that you want to send the pushes to. AppPresser saves device IDs if the app user is a logged in member of your WordPress site, for example in BuddyPress. This will not work unless the user has logged in through your app.     
$recipients = array( 1, 24 );      
$message = 'Hi there!';      
$push = new AppPresser_Notifications_Update;     
$devices = $push->get_devices_by_user_id( $recipients );      
if( $devices ) {        
$push->notification_send( 'now', $message, 1, $devices );     
} 
}

You will want to change the hook, and add your own user IDs to the $recipients array.

Send Push Notifications for a Custom Post Type

This will ignore the preferences and always send a notification. However, in this example you don't need to supply a term as the example below. Users must be subscribed to that category (term) to receive the notification.

$my_custom_subscribe_push = new My_Custom_Subcribe_Push();

/**
 * Like the previous example, this will ignore the preferences and always send a notification.
 * However, in this example you don't limit this to a certain term.
 * User must be subscribed to any associated term to receive the notification.
 */
class My_Custom_Subcribe_Push {

public $message   = 'We have a new Captain\'s Log you might be interest in';
public $post_type = 'captains-log'; // i.e. post

public function __construct() {
add_action( "publish_{$this->post_type}", array( $this, 'push_my_new_posttype_to_subscribers' ), 10, 2 );
add_action( "publish_future_{$this->post_type}", array( $this, 'push_my_new_posttype_to_subscribers' ) );
}

public function push_my_new_posttype_to_subscribers( $post_id, $post = '' ) {

if( $post == '' || ! is_object( $post ) ) {
$post = get_post( $post_id );
}

$taxonomies = get_object_taxonomies( $post );

$post_terms = wp_get_post_terms( $post_id, $taxonomies );

if( $post_terms ) {
// Create an array of terms structured in a way AppPresser_Notifications_Segments expects
$terms_to_match = array();
foreach ($post_terms as $term) {
$terms_to_match[] = array(
'post_type' => $this->post_type,
'taxonomy'  => $term->taxonomy,
'term_id'   => $term->term_id,
);
}

$user_ids = AppPresser_Notifications_Segments::get_user_ids_by_segment_terms( $terms_to_match );
$device_ids = AppPresser_Notifications_Segments::get_appp_push_device_ids( $user_ids );

if( $device_ids ) {
$custom_url = get_permalink( $post_id );
$push = new AppPresser_Notifications_Update();
$push->notification_send( 'now', $this->message, 1, $device_ids, array(), $custom_url );
}
}
}
}

Force pushes to subscribers to a certain post_type, taxonomy and term

It is possible to send notifications to only subscribers to certain custom post types, custom taxonomies and terms. In this example, it will always send notifications regardless of any settings. However, the user must be subscribed to the category.

Example:

add_action( 'init', 'push_to_subscribers_hooks' );

function push_to_subscribers_hooks() {

$post_type = 'product';

add_action( "publish_$post_type", 'push_new_product_to_subscribers', 10, 2 );
add_action( "publish_future_$post_type", 'push_new_product_to_subscribers' );
}

/**
 * This will ignore the preferences to send when published and always send a notification.
 * User must be subscribed to that category to receive the notification.
 */
function push_new_product_to_subscribers( $post_id, $post = '' ) {

$product_id = $post_id;

$message    = 'We have a new coin you might be interest in';
$custom_url = get_permalink( $product_id );

$post_type  = 'product';
$term_slug  = 'coins';
$taxonomy   = 'product_cat';

$term = get_term_by( 'slug', $term_slug, $taxonomy );

if( $term ) {
$terms = array();
$terms[] = array(
'post_type' => $post_type,
'taxonomy'  => $taxonomy,
'term_id'   => $term->term_id,
);

$user_ids = AppPresser_Notifications_Segments::get_user_ids_by_segment_terms( $terms );
$device_ids = AppPresser_Notifications_Segments::get_appp_push_device_ids( $user_ids );

if( $device_ids ) {
$push = new AppPresser_Notifications_Update;
$push->notification_send( 'now', $message, 1, $device_ids, array(), $custom_url );
}
}
}

Example Plugin

Download an example plugin utilizing the above features and more to use in your own projects.

Download Example Customization Plugin