Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/267.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 增加服务器端的徽章号_Php_Ios_Push Notification - Fatal编程技术网

Php 增加服务器端的徽章号

Php 增加服务器端的徽章号,php,ios,push-notification,Php,Ios,Push Notification,我有一张这样的桌子 |------------|-----------| |device_token|badge_count| |------------|-----------| |123456789 |3 | |------------|-----------| |987654321 |2 | |------------|-----------| 我有php来创建两个数组: $query = mysql_query("SELECT * FROM

我有一张这样的桌子

|------------|-----------|
|device_token|badge_count|
|------------|-----------|
|123456789   |3          |
|------------|-----------|
|987654321   |2          |
|------------|-----------|
我有php来创建两个数组:

$query = mysql_query("SELECT * FROM push");
$deviceToken = array();
while($row = mysql_fetch_assoc($query)){
    $deviceToken[] = $row['device_token'];
    $badgeCount[] = $row['badge_count'];
}
然后按如下方式插入它们:

foreach($deviceToken as $key=>$value){
        echo $badgeCount[$key];

        $body['aps'] = array(
            'alert' => $message,
            'sound' => 'default',
            'badge' => $badgeCount[$key],
        );

        $payload = json_encode($body);

        $msg = chr(0) . pack('n', 32) . pack('H*', $value) . pack('n', strlen($payload)) . $payload;
        $result = fwrite($fp, $msg, strlen($msg));

        if (!$result){
            die( 'Message not delivered' . PHP_EOL);
        }
}
这将返回回声
3
2
。但是
$payload
未成功提供正确的“徽章号”

在推送通知中,没有显示徽章编号。如果我将
'badge'=>$badgeCount[$key]
替换为
'badge'=>2
则徽章编号显示为2


知道我做错了什么吗?

返回关联数组:

$query = mysql_query("SELECT device_token, badge_count FROM push");
$devices = array();
while($row = mysql_fetch_assoc($query)){
    $devices[] = $row;
}
通过设备进行循环:

foreach($devices as $device){

        $body['aps'] = array(
            'alert' => $message,
            'sound' => 'default',
            'badge' => (int)$device['badge_count'],//very important cast !!!
        );

        $payload = json_encode($body);

        $msg = chr(0) . pack('n', 32) . pack('H*', $device['device_token']) 
                      . pack('n', strlen($payload)) . $payload;

        $result = fwrite($fp, $msg, strlen($msg));

        if (!$result){
            die( 'Message not delivered' . PHP_EOL);
        }
}

我迷路了。什么是不正确的预期输出和当前输出?我认为
$payload
没有正确编码
$badgeCount[$key]
是否要在每次迭代时附加到
$body['aps']
?为此,您的分配应该是
$body['aps'][]=array(…
我尝试了
$body['aps'][]=array('alert'=>$message,'sound'=>'default','badge'=>$badgeCount[$key],)现在什么都不是了working@Maximilianlol我完成了,我只是想包括,以确保它是铸造到一个INT你认为的标志?而且这似乎不起作用不知道为什么。没有得到任何错误