0

我的代码有一个小问题

    <?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 时,它不起作用!

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

非常感谢

4

1 回答 1

0
  • 根据我的经验,如果您正确完成所有编码和其他工作,但是,在您的数据库中有无效(在测试期间插入)令牌,并在循环中向所有设备发送推送消息,则推送通知将不会在第一次故障后发送/无效的令牌,即使您还有更多好的令牌。所以,请检查一下。
  • APNS-php 是一个高级 API,可以帮助您进行多次发送、检测错误令牌等。好吧,您当然可以不用它,但是测试 Pushnotification 发送几乎需要 90% 的开发时间,而且这个 API 和它很酷的异常处理可以摇滚你的世界!
于 2013-03-07T10:00:49.610 回答