Created
May 6, 2021 07:44
-
-
Save Maxim-Kolmogorov/eea2ed7fac667cf9656bf83faf1a0edc to your computer and use it in GitHub Desktop.
Send push notification from PHP server
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 | |
$url = "https://fcm.googleapis.com/fcm/send"; | |
$tokens = array('token1', 'token2'); // your devices tokens (limit 500) | |
$serverKey = 'ApiKey'; // Server Api Key from Firebase Console | |
$notification = array( | |
'title' => $title, // message title | |
'body' => $body, // message body | |
'sound' => 'default', // sound | |
); | |
$arrayToSend = array( | |
'registration_ids' => $tokens, | |
'notification' => $notification, | |
'priority '=> 'high' | |
); | |
$json = json_encode($arrayToSend); | |
$headers = array( | |
'Content-Type: application/json', | |
'Authorization: key='. $serverKey | |
); | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, $url); | |
curl_setopt($ch, CURLOPT_CUSTOMREQUEST,"POST"); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $json); | |
curl_setopt($ch, CURLOPT_HTTPHEADER,$headers); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
$response = curl_exec($ch); | |
if (!$response) { | |
var_dump('CURL error: ' . curl_error($ch)); | |
die(); | |
} | |
curl_close($ch); | |
$result = json_decode($response, true); | |
if ((int)$result['success'] == 1) | |
{ | |
echo 'Notifications sended successfully!'; | |
} | |
else | |
{ | |
foreach ($result['results'] as $key => $data) | |
{ | |
var_dump($data['error'] . ' for key ' . $tokens[$key]); | |
die(); | |
} | |
} | |
?> |
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 | |
$token = 'DeviceToken'; | |
$apiHost = 'api.development.push.apple.com'; // Only for development purposes. Make sure your application is in a production environment and set the value to "the api.push.apple.com variable" when your app will be available in AppStore | |
$apnsCert = '/path/to/cert.pem; | |
$apnsPass = 'certificate pass'; | |
$payload['aps'] = | |
array( | |
'alert' => [ | |
'title' => $title, | |
'body' => $message | |
], | |
'badge' => 0, | |
'sound' => 'default' | |
); | |
$payload = json_encode($payload); | |
exec('curl -d \''.$payload.'\' --cert '.$apnsCert.':'.$apnsPass.' -H "apns-topic: com.pizzasan" --http2 https://' . $apiHost . '/3/device/'.$token, $output); | |
var_dump($output); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment