推送通知徽章计数未在ios上更新

时间:2015-12-04 07:04:48

标签: php ios push-notification apple-push-notifications

推送通知徽章数不增加。它总是显示1.我使用php从服务器发送推送,这里的代码如下:

    $apnsHost = 'gateway.sandbox.push.apple.com';
    $apnsCert = 'project_dev.pem';

    $apnsPort = 2195;
    $badgecount=1;

    $streamContext = stream_context_create();

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

$apns = stream_socket_client('ssl://' . $apnsHost . ':' . $apnsPort, $error, $errorString, 2, STREAM_CLIENT_CONNECT, $streamContext);
if (!$apns){
    exit("Failed to connect: $error $errorString" . PHP_EOL);
}else{
    $messageHeader = (strlen($message) > 26) ? substr($message,0,26).'...' : $message;

$msgd=explode(':',$message);

$payload['aps'] = array('alert' => $messageHeader,'badge' => $badgecount, 'sound' => 'default','mess'=>$fromUserId.'||'.$userNamee.'||'.$userimage.'||'.$message,'type'=>'reply', 'unread_count'=>$unread_count);

$output = json_encode($payload);
$token = pack('H*', str_replace(' ', '', $token));

$apnsMessage = chr(0) . chr(0) . chr(32) . $token . chr(0) . chr(strlen($output)) .     $output;

fwrite($apns, $apnsMessage);

fclose($apns);
}

2 个答案:

答案 0 :(得分:1)

badgecount总是由服务器端发送。似乎你发送$ badgecount = 1; always.Update with other value然后check.It肯定反映在IOS上。

答案 1 :(得分:0)

在您的代码中,您始终发送徽章,这就是为什么您只能将$ badgecount 1徽章带到应用程序端。

您需要使用以下类型发送通知徽章。

$query = "SELECT badgecount FROM pushnotifications WHERE device_token = '{$device_token}'";
    $query = $this->db->query($query);
    $row = $query->row_array();
    $updatequery = "update pushnotifications set badgecount=badgecount+1 WHERE device_token ='{$device_token}'";
    $updatequery = $this->db->query($updatequery);
    $device = $device_token;
    $payload['aps'] = array('alert' => $pushmessage, 'badge' =>$row["badgecount"]+1, 'sound' => 'default');
    $payload = json_encode($payload); 

我认为您现在可以通过上述代码更加清晰。