簡體   English   中英

推送通知徽章計數未在iOS上更新

[英]Push notification badge count not updating on ios

推送通知徽章計數沒有增加。它始終顯示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);
}

badgecount總是由服務器端發送。看來您正在發送$ badgecount = 1; 總是用其他值更新它然后檢查。它肯定會反映在IOS上。

在您的代碼中,您始終向您發送徽章,這就是為什么您只將$ 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); 

我想您現在可以通過上述代碼更加清晰。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM