iPhone-iOS and Android push notification script - PHP code
Here I am presenting before you - how to perform the push notification process with PHP code which I have recently gone through.
Based on url GET parameter ( "type" ), the script will automatically execute the respective iOS or Android push notification process.
//*** Android Push Notification
if( isset($_GET['type']) && ($_GET['type'] == 'android')){
//*** Your API access key from Google Cloud Messaging( GCM )
define( 'API_ACCESS_KEY', 'YOUR GCM KEY' );
$registrationIds = array("YOUR DEVICE IDs" );
//*** Configure the bundle
$msg = array
(
'message' => 'Your message for push.',
'title' => 'Your title',
'subtitle' => 'Your subtitle',
'tickerText' => 'Your ticker text here',
'vibrate' => 1,
'sound' => 1
);
$fields = array
(
'registration_ids' => $registrationIds,
'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 );
curl_close( $ch );
echo $result;
}
elseif( isset($_GET['type']) && ($_GET['type'] == 'ios') )
{
//*** iPhone/iOS Push Notification
$ctx = stream_context_create();
$passphrase = 'YOUR PASS PHRASE';
$message = 'This is test push.';
$deviceToken = 'DEVICE TOKEN';
stream_context_set_option($ctx, 'ssl', 'local_cert', 'LOCATION TO YOUR .pem FILE');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
$fp = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195', $err, $errstr, 30, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
if (!$fp)
exit("Failed to connect: $err $errstr" . PHP_EOL);
echo 'Connected to APNS' . PHP_EOL;
//*** Configure the payload - Badge 0 will not set the notification badge over your app icon
$body['aps'] = array(
'badge' => +1,
'alert' => $message,
'sound' => 'default'
);
$payload = json_encode($body);
//*** Create binary notification
$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;
$result = fwrite($fp, $msg, strlen($msg));
if (!$result)
echo 'Message could not be delivered' . PHP_EOL;
else
echo 'Message successfully delivered-'.$message. PHP_EOL;
//*** Close the connection to server
fclose($fp);
}
Issues - You may face :
1- If you get the connection issue to gateway for ios push, please check your pem file or either test it with
openssl s_client -connect gateway.sandbox.push.apple.com:2195 -cert your.pem
2- Once you create ipa for your application, Apple assumes it's now on production and so if your are getting issue like : not able to send the push from developer pem with the same code, you need to set the production pem after ipa creation.
Reference:
https://developer.apple.com/library/mac/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/ProvisioningDevelopment.html
Comments
Post a Comment