What is a token of device?
To send push notifications on the IOS device, we need his token. Token it is a unique string for apple account, device and application for which notifications will be sent.
Example of IOS token: 4a9cf2a8eb22e1718f7368982870eb950a7367134d2e2b66cbb6b21773600671
Yii extension to send push notifications
To send push notifications I will use the extension Yii 1.1: apns-gcm. You can download it from the official site extensions yii: http://www.yiiframework.com/extension/apns-gcm/ or from site devreadwrite.com.
Certificate to send push notifications
Before using the extension we need to get a certificate from the IOS Programmer.Or make it yourself, how to generate the certificate, you can search in Google. I received a certificate from the developer IOS with extension p12. Yii 1.1: apns-gcm need certificate with pem of extension, here you can convert p12 to pem: https://www.sslshopper.com/ssl-converter.html. But you can ask for IOS developer to do it.
And so we have a certificate and extension. Now we can connect the extension to the project.
Connecting expansion apns-gcm to send push notifications
Copy the contents of the archive to a folder /protected/extensions/apns-gcm. And add next code in configuration file /protected/config/main.php:
return array( //... 'components' => array( //... 'apns' => array( 'class' => 'application.extensions.apns-gcm.YiiApns', 'environment' => 'sandbox', 'pemFile' => $_SERVER['DOCUMENT_ROOT'] . '/apnssert/apns-dev.pem', 'dryRun' => false, 'options' => array( 'sendRetryTimes' => 5 ), ), 'gcm' => array( 'class' => 'ext.apns-gcm.YiiGcm', 'apiKey' => '' ), 'apnsGcm' => array( 'class' => 'ext.apns-gcm.YiiApnsGcm', ), //... ), //... );
Where, /apnssert/apns-dev.pem - path to certificate.
The verification token before sending push notifications
If user uninstall an application or disable notifications from it, then you must delete him token. Because keep it no longer useful. He will not receive any more notifications.
Sample code for delete of old tokens:
public function actionIosDeleteOldToken() { $apns = Yii::app()->apns; $feedback = new ApnsPHP_Feedback( ApnsPHP_Abstract::ENVIRONMENT_SANDBOX, //ApnsPHP_Abstract::ENVIRONMENT_PRODUCTION, Yii::app()->apns->pemFile ); $feedback->connect(); $deviceTokens = $feedback->receive(); if (!empty($deviceTokens)) { foreach ($deviceTokens as $token) { /** * In response, we have an array in the following format * ['timestamp'] => 1406040206 * ['tokenLength'] => 32 * ['deviceToken'] => 4a9cf2a8eb22e1718f7368982870eb950a7367134d2e2b66cbb6b21773600671 */ //Here we run a sql query for delete the token echo 'Token removed: ' . $token['deviceToken'] . '<br/>'; } } }
Sending push notifications
Example of sending push notifications to the one device (1 tokenн):
public function actionIosSendTest($token) { $message = 'Hello! This is a test push notification:)'; //$token - device token $apns = Yii::app()->apns; $apns->send( $token, $message, array( 'customProperty1' => 'If necessary, you can pass additional ', 'customProperty2' => 'Another additional parameters' ), array( 'sound' => 'default', 'badge' => 1 ) ); }
Mass sending push notifications
Example of sending a push notification to multiple devices (multiple tokens):
public function actionIosSendTestMas() { $message = 'Hello! This is a test push notification for all:)'; //We form an array of tokens $devicesTokens = array('<token1>', '<token2>', 'tokenN'), $apns = Yii::app()->apns; $apns->sendMulti( devicesTokens, $message, array( //Additional parameters ), array( 'sound' => 'default', 'badge' => 1 ) ); }
Later, I will write about how I did send push notifications to Android devices.