已收到iOS推送通知但没有徽章价值

时间:2016-03-01 13:29:48

标签: php ios apple-push-notifications

在我的iOS Swift应用程序中,我可以使用此php代码发送推送通知:

$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));

我成功收到了包含声音的消息,但我的应用图标没有获得徽章价值。

3 个答案:

答案 0 :(得分: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');

答案 1 :(得分:1)

使用'badge' => 1而非'badge' => "1"

答案 2 :(得分:0)

嘿@GhostStack确保您必须以applicationDodFinishLaunchingWithOptions方式注册警报/声音和徽章

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