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

iOS push notification received but no badge value

在我的 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));

我成功收到消息,有声音,但我的应用程序图标没有徽章值。

您正在尝试使用字符串值设置徽章:

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

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

嘿@GhostStack 确保您必须在 applicationDodFinishLaunchingWithOptions 方法

中注册 alert/sound 和徽章
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
 (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];