Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $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.
- $gcmUrl = 'https://android.googleapis.com/gcm/send';
- // Get the device token (fetch from database for example):
- $id = $_POST['id'];
- $name = $_POST['name'];
- $regid = ''; // Token lawan, untuk menerima push notification, contoh bisa di extract dari database
- // Database looping dibawah ini, menampilkan uuid (token) yang sudah disimpan di database pada php yang sebelumnya
- foreach($regid as $list){
- $arr[] = $list->uuid;
- }
- // Set message:
- $message = $_POST['msg'];
- $msg = $name." : ".$message;
- // Send message:
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $gcmUrl);
- curl_setopt($ch, CURLOPT_POST, true);
- curl_setopt($ch, CURLOPT_HTTPHEADER,
- array(
- 'Authorization: key=' . $apiKey,
- 'Content-Type: application/json'
- )
- );
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(
- array(
- 'registration_ids' => $arr,
- 'data' => array(
- 'title' => "SastraHost Chat",
- 'message' => $msg
- )
- ),
- JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP
- )
- );
- $result = curl_exec($ch);
- if ($result === false) {
- throw new \Exception(curl_error($ch));
- }
- curl_close($ch);
- $data = array("res" => "ok","why" => $result.' | '.$arr);
- echo $data;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement