Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP中的多通知推送iPhone_Php_Iphone_Push Notification - Fatal编程技术网

PHP中的多通知推送iPhone

PHP中的多通知推送iPhone,php,iphone,push-notification,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"); mysq

我的代码有点问题

    <?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";
        }
         ?>
  • 根据我的经验,如果您正确地完成了所有编码和其他工作,但是在数据库中有无效(在测试期间插入)令牌,并且在循环中向所有设备发送pushmessages,那么在第一个错误/无效令牌之后将不会发送推送通知,即使您还有更多的好令牌。所以,请检查一下这个
  • 帮助您进行多次发送、检测错误令牌等。当然,您可以不使用它,但Pushnotification发送的测试几乎需要90%的开发时间,而此API及其酷炫的异常处理可以震撼您的世界
这需要首先进行基本调试。您在哪一点丢失代币?到底发生了什么?您收到了什么错误消息?首先,您可以向单个设备发送通知吗?谢谢,我没有错误消息,只是我没有收到通知,thzt可以从数据库中的恢复设备中派生,因为如果我在函数sendPOST中写入,$deviceToken='';它在工作是的,当我发送一个通知时它在工作抱歉,但我不明白…:哪一部分?这与你的要求无关吗?对我来说,查看整个代码并遵循它是很困难的。