-
-
Save zhocker/adc5613d18b27aa50d0c04cf77dfea7a to your computer and use it in GitHub Desktop.
Simple PHP script showing how to send an Android push notification. Be sure to replace the API_ACCESS_KEY with a proper one from the Google API's Console page. To use the script, just call scriptName.php?id=THE_DEVICE_REGISTRATION_ID
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| // API access key from Google API's Console | |
| define( 'API_ACCESS_KEY', 'YOUR-API-ACCESS-KEY-GOES-HERE' ); | |
| $regId = isset($_GET['id']) ? $_GET['id'] : ''; | |
| $title = isset($_GET['title']) ? $_GET['title'] : "ESPER"; //if title not set | |
| $body = isset($_GET['body']) ? $_GET['body'] : "Receive Notification"; //if body not set | |
| $msg = array(); | |
| $msg['title'] = '"' . $title . '"'; //msg must have double quote "msg" | |
| $msg['body'] = '"' . $body . '"'; //msg must have double quote "msg" | |
| $fields = array( | |
| 'to' => $regId, | |
| 'data' => $msg | |
| ); | |
| $headers = array | |
| ( | |
| 'Authorization: key=' . API_ACCESS_KEY, | |
| 'Content-Type: application/json' | |
| ); | |
| $ch = curl_init(); | |
| curl_setopt( $ch,CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send' ); | |
| curl_setopt( $ch,CURLOPT_POST, true ); | |
| curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers ); | |
| curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true ); | |
| curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false ); | |
| curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) ); | |
| $result = curl_exec($ch ); | |
| curl_close( $ch ); | |
| echo $result; |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ผมแก้ไข File PHP สำหรับยิง Notification มาแล้วครับ
เปลี่ยนแค่ YOUR-API-ACCESS-KEY-GOES-HERE เป็น API ที่ให้ไปในตอนแรกครับ
.../fcm.php
Parameter
example:
case one device ยิงไปเครื่องที่ระบุไว้
.../fcm.php?id=
.../fcm.php?id=&title=<title_msg>&body=<body_msg>
case global ยิงไปทุกเครื่อง
id fixed to "/topics/global" << without "" >>>
id=/topics/global.../fcm.php?id=/topics/global
.../fcm.php?id=/topics/global&title=<title_msg>&body=<body_msg>