我可以将通知推送到一个设备,但无法推入循环

时间:2011-12-26 09:41:57

标签: push-notification

我对推送通知服务进行编码,如下所示。它将获得存储在test_db中的令牌,然后发送它。问题是我没有收到所有设备的通知。但是,如果我向一个特定设备发送通知并取消注释**//if( $row['pushtoken']=='bfd53a383e0f65bc96b43f3547548ec13d4c46d61526d7b6be5d7ed581563e77')**,设备将收到通知。这很奇怪。这是我的代码:

$link = mysql_connect('localhost', 'root', '111111');
if (!$link) {
    error_log( "mysql_connect fail\n");
    die('could not connect: ' . mysql_error());
}
else
{
    $is_db_selected = mysql_select_db('test_db',$link);
    if($is_db_selected)
    {
        $sql = "select * from pushtoken;";
        error_log($sql);
        $result = mysql_query($sql, $link);
        if ($result) 
        {
            echo "ready to push notification\n";
            $apnsHost = 'gateway.sandbox.push.apple.com';
            $apnsPort = 2195;
            $apnsCert = 'aps_dev.pem';

            $streamContext = stream_context_create();
            stream_context_set_option($streamContext, 'ssl', 'local_cert', $apnsCert);

            $apns = stream_socket_client('ssl://' . $apnsHost . ':' . $apnsPort, $error, $errorString, 10, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT , $streamContext);
             if (!$apns)
                exit("Failed to connect: $error $errorString" . PHP_EOL);
            echo 'Connected to APNS' . PHP_EOL;
            ob_flush();
            flush();
            $payload['aps'] = array('alert' => 'xman give mortal 7 messages!!!', 'badge' => 1, 'sound' => 'default');
            $payload = json_encode($payload);
            while ($row = mysql_fetch_assoc($result)) 
            {
                ***//if( $row['pushtoken']=='bfd53a383e0f65bc96b43f3547548ec13d4c46d61526d7b6be5d7ed581563e77')***
                {
                echo "push notification one by one\n";
                $deviceToken = $row['pushtoken'];
                $apnsMessage = chr(0) . pack("n", 32) . pack('H*',  $deviceToken) .pack("n", strlen($payload)). $payload;
                if(fwrite($apns, $apnsMessage,strlen($apnsMessage))==FALSE)
                {
                    echo "can't write to socket!<br />";
                }
                    ob_flush();
                flush();
                //fclose($apns);
                echo "token is ".$deviceToken ."\n";
                //break;
                }


            }
            //socket_close($apns);
            fclose($apns);
        } else {
            error_log ('error select pushtoken: ' . mysql_error() . "\n");
        }

    }       
}

1 个答案:

答案 0 :(得分:0)

您可能想要检查所有令牌是否有效。在过去,我有一个问题,如果我在我的数据库中有一个dev令牌并尝试使用生产推送服务器,那么使用dev令牌在消息之后发送的所有消息都将无法传递。