Example 1: php send ios push notification
<?php
$deviceToken = '8845ba7c41e95e12caea6381ea6f01b5cd7b59a52feb9005e0727a65a4105dc2a0';
$passphrase = '';
$message = 'Your message';
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
$fp = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
if (!$fp)
exit("Failed to connect: $err $errstr" . PHP_EOL);
echo 'Connected to APNS' . PHP_EOL;
$body['aps'] = array(
'alert' => array(
'body' => $message,
'action-loc-key' => 'Bango App',
),
'badge' => 2,
'sound' => 'oven.caf',
);
$payload = json_encode($body);
$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;
$result = fwrite($fp, $msg, strlen($msg));
if (!$result)
echo 'Message not delivered' . PHP_EOL;
else
echo 'Message successfully delivered' . PHP_EOL;
fclose($fp);
Example 2: how to send notification to android from php
<?php
$apiKey = "your api key";
$registrationIDs = array( "reg id1","reg id2");
$message = "Your message e.g. the title of post";
$url = 'https://android.googleapis.com/gcm/send';
$fields = array(
'registration_ids' => $registrationIDs,
'data' => array( "message" => $message ),
);
$headers = array(
'Authorization: key=' . $apiKey,
'Content-Type: application/json'
);
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $url);
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;
?>
Example 3: send notification php to PC
use Joli\JoliNotif\Notification;
if ($notifier) {
$notification =
(new Notification())
->setBody('The notification body')
->setTitle('The notification title')
->setIcon(__DIR__.'/Resources/icons/success.png');
;
$notifier->send($notification);
}