Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rhafiko/6145dbe359e2858f5b0122e850858aa6 to your computer and use it in GitHub Desktop.
Save rhafiko/6145dbe359e2858f5b0122e850858aa6 to your computer and use it in GitHub Desktop.
forked from MohammadaliMirhamed/PhpFireBaseNotificationSample.php -Simple PHP FireBase (FCM) script showing how to send an Android push notification. -Be sure to replace the SERVER_API_ACCESS_KEY with a proper one from the Google API's Console page. -Create project on firebase console, It returns me Server Accesss key and Legacy key under Projec…
<?php
#API access key from Google API's Console
define( 'API_ACCESS_KEY', 'YOUR-SERVER-API-ACCESS-KEY-GOES-HERE' );
$registrationIds = $_GET['id'];
$topic = $_GET['topic'];
#prep the bundle
$msg = array
(
'body' => 'Body Of Notification',
'title' => 'Title Of Notification',
'icon' => 'myicon',
'sound' => 'mySound'
);
$fields = array
(
'notification' => $msg
);
if ($topic){
$fields = array_merge($fields, array('condition' => "'$topic' in topics"));
}else if ($registrationIds){
$fields = array_merge($fields, array('to' => $registrationIds));
}else{
echo "No topic or target device ID was informed. Please review your parameters!";
exit;
}
print_r( $fields);
$headers = array
(
'Authorization: key=' . API_ACCESS_KEY,
'Content-Type: application/json'
);
#Send Reponse To FireBase Server
$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 Of FireBase Server
echo $result;
@rhafiko
Copy link
Author

rhafiko commented Sep 11, 2019

forked from MohammadaliMirhamed/PhpFireBaseNotificationSample.php
-Simple PHP FireBase (FCM) script showing how to send an Android push notification.
-Be sure to replace the SERVER_API_ACCESS_KEY with a proper one from the Google API's Console page.
-Create project on firebase console, It returns me Server Accesss key and Legacy key under Project Overview-> project settings -> Cloud Message.
-To use the script, just call http:///PhpFireBaseNotificationSample.php?id=THE_DEVICE_REGISTRATION_ID&topic=YOU_FIREBASE_TOPIC

What's the difference from the original script ?
-You can send message to a specific device by informing a device ID to the parameter 'id'
-or send a broadcast to a specific topic to the parameter 'topic'.
-it is necessary to set a specific topic previously on your Firebase Console and
also the device must subscribe to it.
-You will also receive a error message if you do not pass any value to those two needed parameters.

@MohammadaliMirhamed
Copy link

Dear ThankYou so much. you made it too usefull . thank you bro .

https://mirhamedrooy.ir

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