多个通知在PHP中推送iPhone

时间:2013-03-07 09:48:24

标签: php iphone push-notification

我的代码中有一点问题

    <?php
        define("DB_HOST","");
        define("DB_USER","");
        define("DB_PASSWORD","");
        define("DB_DATABASE","");

        $con = mysql_connect(DB_HOST,DB_USER,DB_PASSWORD) or die("erreur connection");
        mysql_select_db(DB_DATABASE);

        //Make $i Global and set it to zero
        global $i;
        $i=0;
       //Make $j Global and set it to zero
        global $j;
        $j=0;
        //Makes $message global and defines the text for the message
        global $message;
        $message = "Hello everybody";
        //Gets the device token
        $result = mysql_query("select token FROM users_iphone");
        while($row = mysql_fetch_array($result))
        {
          //Gets the message string
          GLOBAL $message;
          //Gets the devicetoken from the database and sets a string with the token
          $devicetoken = $row['deviceToken'];
          //Calls the function to send the message and defines parameter for the function. In this case the device token and the message to be sent.
          sendPOST($devicetoken, $message);
          //Gets $i, defined earlier and adds 1 to it for each device token in our database.  We are counting how many devicetokens we have.
          global $i;
          $i++;
       }
       //Function that sends our push notification
        function sendPOST ($deviceToken, $messageGlobal){
        $deviceToken = '<5398e918 2c303556 23dfff5e 2754ff54 e55106f9 8d07ea4b 96cd88ba 288f526f>';
    $deviceToken = str_replace('<', '', $deviceToken);
    $deviceToken = str_replace('>', '', $deviceToken);
    $deviceToken = str_replace(' ', '', $deviceToken);
    //token is good with no space and no > and <

$passphrase = 'mypassphrase';
        //start of apns code
       $ctx = stream_context_create();
       stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
       stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
        $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 to connect: $err $errstr" . PHP_EOL);

        $body['aps'] = array(
    'alert' => $messageGlobal,
    'sound' => 'default'
    );
        $payload = json_encode($body);
        $msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;
       $result = fwrite($fp, $msg, strlen($msg));
         if (!$result)
    echo 'Message not delivered' . PHP_EOL;
       //end of apns code
       //gets $j and adds 1  
       global $j;
    $j++;
       fclose($fp);
       }
       //checks if $j is equal to $i.  If so it means all the messages were delivered to apns (This doesn't mean that all the messages will be delivered by apple.  It does mean that they were delivered to apple from us.
        if ($j==$i){
        echo "<h1><b>Message's Successfully delivered.</b></h1><br>";

        echo "<b>Message:</b> $message<br>";
        echo "<b>Messages Delivered: </b>$j out of $i";
        }
        //this checks to see if $i is greater that $j.  If not then that means something went wrong on our end.  Therefore we have not delivered all the messages from our end to apple successfully.  This could mean a bad connection or server failure/error.
        elseif ($i>$j){
        echo "<h1><b>Not all messages have been delivered (at least on our end).  $j out of $i messages were delivered on our end.</b></h1>";

            echo "<b>Message:</b> $message";
          echo "<b>Messages Delivered: </b>$j out of $i";
        }
         ?>

当我在我的php文件中直接注意$ deviceToken时,会收到通知,但是当我想在我的数据库中恢复deviceToken时,它不起作用!

请帮帮我,我的代码包含一些问题?

非常感谢

1 个答案:

答案 0 :(得分:0)

  • 根据我的经验,如果你正确地完成所有编码和其他工作,但是,在数据库中有无效(在测试期间插入)令牌,并在循环中向所有设备发送推送消息,推送即使您还有更多好的令牌,通知也不会在第一个错误/无效令牌后发出。所以,请检查一下。
  • APNS-php is an advanced API可以帮助您进行多次发送,检测错误的令牌等等。当然,您可以不使用它,但是推送发送测试需要花费近90%的开发时间,而且这个API和它很酷的异常处理可以摇滚你的世界!