Send push notification to firebase (custom module)

Public

.module file

Get raw version
php
  1. // API access key from Google API's Console
  2. define( 'API_ACCESS_KEY_FIREBASE', '*******************************************' );
  3.  
  4.  
  5. function my_module_firebase_menu() {
  6. $items['api/v1/push/send'] = array(
  7. 'title' => 'NEW GCM',
  8. 'page callback' => 'my_module_firebase_send',
  9. 'access callback' => TRUE,
  10. 'delivery callback' => 'drupal_json_output',
  11. );
  12.  
  13. return $items;
  14. }
  15.  
  16. function my_module_firebase_send() {
  17.  
  18. if(isset($_POST['user_id'])) {
  19. $user_id = (int) $_POST['user_id'];
  20. $data = array('test' => 123,);
  21. $output = FirebaseOperations::send($user_id, NULL);
  22. }
  23.  
  24. return $output;
  25. }
  26.  
  27.  
  28. class Push {
  29. static function get_push($push = NULL) {
  30. // this uses the post method
  31. if(empty($_POST)) {
  32. if(isset($_POST['title'])) {
  33. $new_title = $_POST['title'];
  34. $push['title'] = $new_title;
  35. }
  36.  
  37. if(isset($_POST['body'])) {
  38. $new_body = $_POST['body'];
  39. $push['body'] = $new_body;
  40. }
  41. if(isset($_POST['title']) || isset($_POST['body'])) {
  42. $push['sound'] = 1;
  43. $push['tickerText'] = 'You have a new secure message at the my_module.';
  44. $push['priority'] = 'high'; // Set priority for Android/iPhone (iPhone this is set to 10).
  45. return $push;
  46. }
  47. } elseif(!empty($push)) {
  48. if(isset($push['title']) && isset($push['body'])) {
  49. $push['sound'] = 1;
  50. $push['tickerText'] = 'You have a new secure message at the my_module.';
  51. $push['priority'] = 'high'; // Set priority for Android/iPhone (iPhone this is set to 10).
  52. return $push;
  53. }
  54. }
  55. }
  56. function set_title($new_title) {
  57. $this->title = $new_title;
  58. }
  59.  
  60. function set_body($new_body) {
  61. $this->body = $new_body;
  62. }
  63.  
  64. function get_title() {
  65. return $this->title;
  66. }
  67.  
  68. function get_body() {
  69. return $this->body;
  70. }
  71. }
  72.  
  73. class FirebaseOperations {
  74.  
  75. static function send($people, $push = NULL, $data = NULL) {
  76. $to = FirebaseUsers::get_firebase_ids($people['to']);
  77. $push = Push::get_push($push);
  78. $data = FirebaseOperations::data($data);
  79. $fields = FirebaseOperations::fields($to, $push, $data);
  80. $headers = FirebaseOperations::headers();
  81. $output = FirebaseOperations::fire($headers, $fields);
  82.  
  83. return $output;
  84. }
  85.  
  86. static function data($data) {
  87. if(!empty($data)) {
  88. // pass through
  89. } elseif(isset($_POST['data'])) {
  90. $data = $_POST['data'];
  91. }
  92.  
  93. return $data;
  94. }
  95.  
  96. static function fields($to, $push, $data) {
  97. $fields['registration_ids'] = $to; // Who the this FCM is headed out to.
  98. $fields['notification'] = $push; // This is what is visible to the user.
  99. $fields['data'] = $data; // This is what is visible to the device.
  100.  
  101. return $fields;
  102. }
  103.  
  104. static function headers() {
  105. $headers = array(
  106. 'Authorization: key=' . API_ACCESS_KEY_FIREBASE,
  107. 'Content-Type: application/json'
  108. );
  109. return $headers;
  110. }
  111.  
  112. static function fire($headers, $fields) {
  113. $ch = curl_init();
  114. curl_setopt( $ch,CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send' );
  115. curl_setopt( $ch,CURLOPT_POST, true );
  116. curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
  117. curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
  118. curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
  119. curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );
  120. $result = curl_exec($ch );
  121. curl_close( $ch );
  122.  
  123. return $result;
  124. }
  125. }
  126.  
  127. class FirebaseUsers {
  128. static function get_firebase_ids($to_array) {
  129. $array = array();
  130. foreach ($to_array as $key => $user_id) {
  131. $query = 'SELECT d.registration_id FROM {client_device} d WHERE d.user_id = :user_id';
  132. $args = array(
  133. ':user_id' => $user_id,
  134. );
  135. $results = db_query($query, $args);
  136.  
  137. foreach ($results as $key => $value) {
  138. $array[] = $value->registration_id;
  139. }
  140. }
  141.  
  142. return $array;
  143. }
  144. }