Skip to content

Instantly share code, notes, and snippets.

@zhocker
Forked from prime31/gist:5675017
Last active June 20, 2017 08:08
Show Gist options
  • Select an option

  • Save zhocker/adc5613d18b27aa50d0c04cf77dfea7a to your computer and use it in GitHub Desktop.

Select an option

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
<?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;
@zhocker
Copy link
Author

zhocker commented Jun 20, 2017

ผมแก้ไข File PHP สำหรับยิง Notification มาแล้วครับ
เปลี่ยนแค่ YOUR-API-ACCESS-KEY-GOES-HERE เป็น API ที่ให้ไปในตอนแรกครับ

.../fcm.php

Parameter

  1. id >> required
  2. title >> optional
  3. body >> optional

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>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment