Implementing Push notifications for iOS (Server Side)

52,229

Solution 1

Have a look at easyAPNS if you want to host it yourself, or visit Urban Airship if you are ok with a hosting service (they have an extensive set of documentation)

Another good site for info is Ray Wenderlich's site which hosts a 2 part tutorial:

Solution 2

  // Push Notification code for IPHONE in PHP 
  $deviceToken = $users_rows['gcm_regid'];
    $passphrase = 'pass1234';
    $ctx = stream_context_create();
    stream_context_set_option($ctx, 'ssl', 'local_cert', 'DrinksterDevelopment.pem');
    stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);

    $fp = stream_socket_client(
        'ssl://gateway.sandbox.push.apple.com:2195', $err,
        $errstr, 120, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);

    if (!$fp)
        exit("Failed to connect: $err $errstr" . PHP_EOL);

    echo 'Connected to APNS' . PHP_EOL;

    $body['aps'] = array(
       // 'alert' => $_GET["message"].'#'.$_GET["type"].'#'.$_GET["deal_id"],
       'alert' => $_GET["message"],
        'sound' => 'default'
        );
    $body['other'] = $_GET["type"].'#'.$_GET["deal_id"];

    $payload = json_encode($body);
    $msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;
    $result_iphone = fwrite($fp, $msg, strlen($msg));

    if (!$result_iphone)
        $msg_iphone = 'Message not delivered' . PHP_EOL;

    else
        $msg_iphone = 'Message successfully delivered' . PHP_EOL;

     mail('[email protected]', 'IOSPushMsgStatus', $msg_iphone);
     fclose($fp);
    } //if($users_rows['Platform'] == 'Web' OR $users_rows['Platform'] == 'Android')
Share:
52,229
Alex1987
Author by

Alex1987

Updated on May 19, 2020

Comments

  • Alex1987
    Alex1987 about 4 years

    We want to be able to push simple text messages to ALL our iphone users. For that we obviously need to create a server side code that stores the device tokens and pushes the messages whenever necessary. Is there any good example on doing this? (Talking about the server code)

    Thanks