Advertisement
rachmadi

FCM Send

Jul 26th, 2016
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.22 KB | None | 0 0
  1. <?php
  2. /*  
  3. Parameter Example
  4.     $data = array('post_id'=>'12345','post_title'=>'A Blog post');
  5.     $target = 'single tocken id or topic name';
  6.     or
  7.     $target = array('token1','token2','...'); // up to 1000 in one request
  8. */
  9. public function sendMessage($data,$target){
  10. //FCM api URL
  11. $url = 'https://fcm.googleapis.com/fcm/send';
  12. //api_key available in Firebase Console -> Project Settings -> CLOUD MESSAGING -> Server key
  13. $server_key = 'PASTE_YOUR_SERVER_KEY_HERE';
  14.            
  15. $fields = array();
  16. $fields['data'] = $data;
  17. if(is_array($token)){
  18.     $fields['registration_ids'] = $target;
  19. }else{
  20.     $fields['to'] = $target;
  21. }
  22. //header with content_type api key
  23. $headers = array(
  24.     'Content-Type:application/json',
  25.   'Authorization:key='.$server_key
  26. );
  27.            
  28. $ch = curl_init();
  29. curl_setopt($ch, CURLOPT_URL, $url);
  30. curl_setopt($ch, CURLOPT_POST, true);
  31. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  32. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  33. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  34. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  35. curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
  36. $result = curl_exec($ch);
  37. if ($result === FALSE) {
  38.     die('FCM Send Error: ' . curl_error($ch));
  39. }
  40. curl_close($ch);
  41. return $result;
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement