Sending notification using PHP to IOS devices -


i have app receives push notification build on xcode using objective-c. did follow tutorial this tutorial ray wenderlich, notification work fine , can receive immediate message. have own web site , want use send notification using php. did try find tutorial not.

is possible use web site send notification? can use php file send notification? need change ssl certificate production ssl?

her php code use @ moment:

<?php  $devicetoken = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'; // put private key's passphrase here: $passphrase = 'xxxxxxxxxx';  // put alert message here: $message = 'hello app';    $ctx = stream_context_create(); stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem'); stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);  // open connection apns server $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 connect: $err $errstr" . php_eol);  echo 'connected apns' . php_eol;  // create payload body $body['aps'] = array( 'alert' => $message, 'sound' => 'default' );  // encode payload json $payload = json_encode($body);  // build binary notification $msg = chr(0) . pack('n', 32) . pack('h*', $devicetoken) . pack('n', strlen($payload)) . $payload;  // send server $result = fwrite($fp, $msg, strlen($msg));  if (!$result) {    echo 'message not delivered' . php_eol; } else {    echo 'message delivered' . php_eol;  }  // close connection server     fclose($fp);  ?> 

i hope question clear, , hope 1 give me information or direct me tutorial me.

thanks

yes can send push notifications ios devices using php. before sending notification, needing:

1) apple push certificate (apns certificate) - can generated apple developer center. note certificate per app , there 2 kind of certificates, development , distribution. follow this link on how generate one.

2) device token device - done on ios app. code on ios app might this.

- (void)application:(uiapplication*)application didregisterforremotenotificationswithdevicetoken:(nsdata*)devicetoken {     self.storeddevicetoken = [[devicetoken description] stringbytrimmingcharactersinset: [nscharacterset charactersetwithcharactersinstring:@"<>"]];     self.storeddevicetoken = [self.storeddevicetoken stringbyreplacingoccurrencesofstring:@" " withstring:@""];     nslog(@"my token is: %@", self.storeddevicetoken); } 

now have tools required push notification, can start php script.

// put device token here (without spaces): $devicetoken = 'xxxxxxxxxxxxxxx';  $gateway = 'ssl://gateway.push.apple.com:2195';  // put private key's passphrase here: $passphrase = 'yourpassphrase!!';  // put alert message here: $message = 'yoooo! what\'s man!';  $ctx = stream_context_create(); stream_context_set_option($ctx, 'ssl', 'local_cert', 'pathtogeneratedcertificateonstep1'); stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);  // open connection apns server $fp = stream_socket_client(     $gateway, $err,     $errstr, 60, stream_client_connect|stream_client_persistent, $ctx);  if (!$fp)     exit("failed connect: $err $errstr" . php_eol);  echo 'connected apns' . php_eol;  // create payload body $body['aps'] = array(     'alert' => $message,     'sound' => 'default'     );  // encode payload json $payload = json_encode($body);  // build binary notification $msg = chr(0) . pack('n', 32) . pack('h*', $devicetoken) . pack('n', strlen($payload)) . $payload;  // send server $result = fwrite($fp, $msg, strlen($msg));  if (!$result)     echo 'message not delivered' . php_eol; else     echo 'message delivered' . php_eol;  // close connection server fclose($fp); 

for more details on payloads supported apple push notification visit.

notes:

  • if installing ios app manually testing device token valid sandbox notification. so, $gateway 'ssl://gateway.sandbox.push.apple.com:2195'

  • notification cannot received on simulator.