Push Notification For iOS
Push Notification is a message text that pops up on a mobile device Like Android , iOS. We can send notification any time users don't have to be in the app or using their devices to receive them.
This Php script will showing , How to Send Push Notification in IOS and Android Devices, Be Sure to Change API Access Key.
Push Notification Script for iOS Devices :
$deviceToken = "YOUR DEVICE TOKEN ID";
$message = "Wow New Notification ... ";
$ctx = stream_context_create();
// ck.pem is your certificate file
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck_dash.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', '123456');
// Open a connection to the APNS server
$fp = stream_socket_client('ssl://gateway.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
// Check that we've connected
if (!$fp) {
die("Failed to connect: $err $errstr");
}
echo 'Connected to APNS' . PHP_EOL;
// Create the payload body
$body['aps'] = array(
'alert' => array(
'body' => $message
),
'badge' => 1,
'sound' => 'oven.caf',
);
// Encode the payload as JSON
$payload = json_encode($body);
// Build the binary notification
$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;
// Send it to the server
$result = fwrite($fp, $msg, strlen($msg));
if (!$result)
echo 'Message not delivered' . PHP_EOL;
else
echo 'Message successfully delivered' . PHP_EOL;
// Close the connection to the server
fclose($fp);
----------------------------------------------------------------------------------------------------------
Push Notification For Android Devices :
$deviceToken = "YOUR DEVICE TOKEN ID";
$msg = array (
'badge' => +1,
'title' => 'Wow New Notification ... ',
'subtitle' => 'Php Blog',
'tickerText' => 'Tutorial Section',
'vibrate' => 0,
'sound' => 0,
'device_id' => $device_token
);
$fields = array (
'registration_ids' => $device_token,
'data' => $msg
);
$headers = array (
'Authorization: key=' . API_ACCESS_KEY,
'Content-Type: application/json'
);
$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, 'https://android.googleapis.com/gcm/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 );
print_r($result); // Your Results
----------------------------------------------------------------------------------------------------------