Advertisement
mjniuz

Push PHP

Aug 24th, 2015
875
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.55 KB | None | 0 0
  1.         $apiKey = ''; // API key from Google Developer Console, silahkan buat "Crediential" pada google developer yang sudah anda buat, dan buat server key, biarkan IP kosong, dan copy API key nya disini.
  2.         $gcmUrl = 'https://android.googleapis.com/gcm/send';
  3.          
  4.         // Get the device token (fetch from database for example):
  5.         $id  = $_POST['id'];
  6.         $name = $_POST['name'];
  7.         $regid = ''; // Token lawan, untuk menerima push notification, contoh bisa di extract dari database
  8.         // Database looping dibawah ini, menampilkan uuid (token) yang sudah disimpan di database pada php yang sebelumnya
  9.         foreach($regid as $list){
  10.             $arr[] = $list->uuid;
  11.         }
  12.         // Set message:
  13.         $message = $_POST['msg'];
  14.         $msg = $name." : ".$message;
  15.         // Send message:
  16.         $ch = curl_init();
  17.         curl_setopt($ch, CURLOPT_URL, $gcmUrl);
  18.         curl_setopt($ch, CURLOPT_POST, true);
  19.         curl_setopt($ch, CURLOPT_HTTPHEADER,
  20.             array(
  21.                 'Authorization: key=' . $apiKey,
  22.                 'Content-Type: application/json'
  23.             )  
  24.         );
  25.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  26.         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  27.         curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(
  28.                 array(
  29.                     'registration_ids' => $arr,
  30.                     'data' => array(
  31.                         'title' => "SastraHost Chat",
  32.                         'message' => $msg
  33.                     )
  34.                 ),
  35.                 JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP
  36.             )
  37.         );
  38.          
  39.         $result = curl_exec($ch);
  40.         if ($result === false) {
  41.             throw new \Exception(curl_error($ch));
  42.         }
  43.          
  44.         curl_close($ch);
  45.         $data = array("res" => "ok","why" => $result.' | '.$arr);
  46.         echo $data;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement