2016-03-01 121 views
0

在我的iOS斯威夫特程序,我可以發送推送通知這個PHP代碼:的iOS推送通知接收,但沒有徽章值

$ctx = stream_context_create(); 
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem'); 
stream_context_set_option($ctx, 'ssl', 'passphrase', 'APPNAME'); 

// Open a connection to the APNS server 
$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' => 'This is a test', 'badge' => "1", 'sound' => 'default'); 
$payload = json_encode($body); 
$msg = chr(0) . pack('n', 32) . pack('H*', DEVICE_TOKEN) . pack('n', strlen($payload)) . $payload; 
$result = fwrite($fp, $msg, strlen($msg)); 

我順利地收到消息,有聲音,但我的應用程序的圖標就會沒有徽章值。

回答

1

您正在試圖用字符串值設置你的徽章:

$body['aps'] = array('alert' => 'This is a test', 'badge' => "1", 'sound' => 'default'); 

根據有效載荷鍵從Apple documentation的描述,關鍵badge關聯的值必須是數字。用一個整數值代替它:

$body['aps'] = array('alert' => 'This is a test', 'badge' => 1, 'sound' => 'default'); 
+0

註冊唉唉好的 - 這是我的錯誤。現在它工作! thx,但我可以設置動態值嗎?對於用戶得到2個通知的情況,我想設置實際的徽章號碼+ 1 – GhostStack

+0

如果應用程序沒有啓動,您可以只設置值,計數必須保存在服務器端。如果應用程序已啓動,則可以設置'UIApplication'的applicationIconBadgeNumber屬性(對於可以從客戶端計算的本地通知很有用)。 –

1

使用'badge' => 1而不是'badge' => "1"

0

嘿@GhostStack請確保您有警報/聲音和徽章applicationDodFinishLaunchingWithOptions方法

[[UIApplication sharedApplication] registerForRemoteNotificationTypes: 
(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];