2016-03-01 135 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)];