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";
        }
         ?>

$deviceToken を php ファイルに直接書き留めると通知が届きますが、データベースで deviceToken を回復したい場合は機能しません!

助けてください、コードに問題がありますか?

どうもありがとう

4

1 に答える 1

0
  • From my experience, if you do all your coding and other stuffs right, BUT, have invalid(inserted during testing) tokens inside your database, and sending pushmessages to all devices in a loop, Push notifications WILL NOT BE SENT after the 1st faulty/invalid token, even if you have many more good tokens left. So, please check on this.
  • APNS-php is an advanced API to help you with multiple sending, detecting faulty tokens etc. Well, ofcourse you can go without it, but testing of Pushnotification sending takes almost 90% of development time, and this API and it's cool exception handling can rock your world!
于 2013-03-07T10:00:49.610 に答える