Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- Snippet: Push notifications with WordApp
- Author: Dave A Sargent
- Author URI: http://app-developers.biz
- Distributed under the GPLv2
- */
- $device_token_arr[] = self::get_user_device_token($user_id);
- $args['device_tokens'] = $device_token_arr;
- sendPushNotification($args);
- function get_user_device_token($user_id)
- {
- if(!$user_id)return '';
- return get_user_meta( $user_id, 'wordapp_push_device_token', true);
- }
- function sendPushNotification($args){
- if($args['device_tokens']){
- /* CLEAN UUID */
- $final_arr = array_chunk($args['device_tokens'],500);
- for($i=0;$i<count($final_arr);$i++){
- foreach($final_arr[$i] as $final_arrObj){
- if($final_arrObj && $final_arrObj['device_token']){
- $tokens = $final_arrObj['device_token'];
- $platform = $final_arrObj['platform'];
- if($final_arrObj['username']){ $args['username'] = $final_arrObj['username']; }
- }else{
- $tokens = $final_arrObj;
- $platform = 0;
- }
- $message = 'message to send here';
- $title = sanitize_text_field($message);
- $wadate = date('Y-m-d H:i:s');
- $content = '';
- $page = array(
- 'post_title' => $title,
- 'post_content' => $content, //blank
- 'post_status' => 'publish',
- 'post_type' => 'wa_pns_messages',
- 'post_date' => $wadate,
- 'post_date_gmt' => $wadate,
- 'post_author' => '1'
- );
- // Page doesn't exist, so lets add it
- $post_id = wp_insert_post( $page );
- add_post_meta($post_id, '_wapn_message', 'no');
- $device_token_ids = array();
- foreach($args['device_tokens'] as $device_token){
- $device_id = get_page_by_title( $device_token, '', 'wa_pns' );
- $device_token_ids[] = $device_id->ID;
- }
- $jsonTitles = json_encode($device_token_ids, false);
- add_post_meta($post_id, '_wapn_message_users', $jsonTitles);
- add_post_meta($post_id, '_wapn_message_users_count', count($final_arr));
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement