Advertisement
Guest User

Untitled

a guest
Jun 2nd, 2017
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.06 KB | None | 0 0
  1. <?php
  2. /*
  3. Snippet: Push notifications with WordApp
  4. Author: Dave A Sargent
  5. Author URI: http://app-developers.biz
  6. Distributed under the GPLv2
  7. */
  8. $device_token_arr[] = self::get_user_device_token($user_id);
  9.  
  10. $args['device_tokens'] = $device_token_arr;
  11.  
  12. sendPushNotification($args);
  13.  
  14.  
  15. function get_user_device_token($user_id)
  16. {
  17. if(!$user_id)return '';
  18.  
  19. return get_user_meta( $user_id, 'wordapp_push_device_token', true);
  20. }
  21.  
  22.  
  23.  
  24.  
  25. function sendPushNotification($args){
  26. if($args['device_tokens']){
  27.  
  28. /* CLEAN UUID */
  29. $final_arr = array_chunk($args['device_tokens'],500);
  30. for($i=0;$i<count($final_arr);$i++){
  31. foreach($final_arr[$i] as $final_arrObj){
  32. if($final_arrObj && $final_arrObj['device_token']){
  33. $tokens = $final_arrObj['device_token'];
  34. $platform = $final_arrObj['platform'];
  35. if($final_arrObj['username']){ $args['username'] = $final_arrObj['username']; }
  36. }else{
  37. $tokens = $final_arrObj;
  38. $platform = 0;
  39. }
  40.  
  41.  
  42. $message = 'message to send here';
  43. $title = sanitize_text_field($message);
  44. $wadate = date('Y-m-d H:i:s');
  45. $content = '';
  46.  
  47. $page = array(
  48. 'post_title' => $title,
  49. 'post_content' => $content, //blank
  50. 'post_status' => 'publish',
  51. 'post_type' => 'wa_pns_messages',
  52. 'post_date' => $wadate,
  53. 'post_date_gmt' => $wadate,
  54. 'post_author' => '1'
  55. );
  56.  
  57. // Page doesn't exist, so lets add it
  58. $post_id = wp_insert_post( $page );
  59.  
  60.  
  61. add_post_meta($post_id, '_wapn_message', 'no');
  62.  
  63.  
  64. $device_token_ids = array();
  65. foreach($args['device_tokens'] as $device_token){
  66.  
  67. $device_id = get_page_by_title( $device_token, '', 'wa_pns' );
  68.  
  69. $device_token_ids[] = $device_id->ID;
  70. }
  71.  
  72. $jsonTitles = json_encode($device_token_ids, false);
  73.  
  74. add_post_meta($post_id, '_wapn_message_users', $jsonTitles);
  75.  
  76. add_post_meta($post_id, '_wapn_message_users_count', count($final_arr));
  77.  
  78.  
  79. }
  80. }
  81. }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement