loops - PHP For iOS Push Notifications, Sometimes More Than 1 Device -
i building app in using nsuserdefaults store various pieces of information, including devicetoken used apns on ios. way working user submits request, , adds entry xml, 1 of elements in xml called devicetoken. when responds request, triggers php @ bottom of page send push notification devicetoken of particular request. issue people have more 1 device, , nice send push notification of owner's devices. can see in code below, have 2 areas devicetokens. if xml contains 2 device tokens, great. issue ones there 1 token, fail deliver one. how can fix php able accept 2 devicetokens, still deliver if 1 available?
<?php $first_name = $_post['first_name']; $last_name = $_post['last_name']; $title = $_post['title']; $devicetoken = $_post['devicetoken']; $devicetoken2 = $_post['devicetoken2']; $xml = simplexml_load_file("http://www.url.xml") or die("not loaded!\n"); $passphrase = 'passphrase'; // put alert message here: $message = 'someone responded you!'; //////////////////////////////////////////////////////////////////////////////// $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.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; $msg2 = chr(0) . pack('n', 32) . pack('h*', $devicetoken2) . pack('n', strlen($payload)) . $payload; // send server $result = fwrite($fp, $msg, strlen($msg)); $result = fwrite($fp, $msg2, strlen($msg2)); if (!$result) echo 'message not delivered' . php_eol; else echo 'message delivered' . php_eol; // close connection server fclose($fp); ?>
what can use function generic one: is, accepts device token object. single string, or array of multiple strings.
then, check whether$devicetoken
array using is_array
function. ref:http://us3.php.net/manual/en/function.is-array.php
for this, may have rewrite small bits of code. overall, this:
if(is_array($devicetoken)) { // iterate on array , call same function each string return; } // you're code send push notification have above.
Comments
Post a Comment