在ios7中更新推送通知的徽章计数

我正在开发推送通知应用程序,但通知到来时徽章数量不会增加.
我在堆栈溢出中看到了很多例子但是没有人有用.

任何人都可以建议我如何解决这个问题…提前谢谢!

我的服务器端PHP代码:

<?php

// Put your device token here (without spaces):
$deviceToken = 'c0d35d5f5ab179f0a93cb7c6b89b96b305ad3517b24e454abd4517e2323f4a7a';

// Put your private key's passphrase here:
$passphrase = '12345push';

// Put your alert message here:
$message = 'My First push notification!';
//$badge = 3;
////////////////////////////////////////////////////////////////////////////////

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

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

echo 'Connected to APNS' . PHP_EOL;

// Create the payload body
$body['aps'] = array(
'alert' => $message,
'sound' => 'default',
    'badge'=>($badge != (null) ? $badge + 1 : 1)
);

// Encode the payload as JSON
$payload = json_encode($body);

// Build the binary notification
$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;

// Send it to the server
$result = fwrite($fp, $msg, strlen($msg));

if (!$result)
echo 'Message not delivered' . PHP_EOL;
else
echo 'Message successfully delivered' . PHP_EOL;

// Close the connection to the server
  fclose($fp);

在appdelegate.m

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo

{
 NSString* alertValue = [[userInfo valueForKey:@"aps"] valueForKey:@"badge"];
 NSLog(@"my message-- %@",alertValue);
 int  badgeValue= [alertValue intValue];
 [UIApplication sharedApplication].applicationIconBadgeNumber += badgeValue;
 }
最佳答案
通常在所有应用中,未读通知计数都在服务器中维护.当服务器向特定设备发送推送通知时,令牌服务器会发送徽章计数以及有效负载.

您的服务器逻辑需要跟踪正确的徽章计数并适当地发送它.

{
    "aps" :  
    {
        "alert" : "Your notification message",
        "badge" : badgecount ,
        "sound" : "bingbong.aiff"
    }
}

编辑

您已在didReceiveRemoteNotification方法中设置徽章计数.之前这个方法叫做appbadgeis设置来自pushnotification,所以从服务器你必须设置正确的徽章..

解:

所以在该webservice中创建一些webservice发送deviceToken和currentBadge以存储在服务器上,并在下次发送push时检查令牌的最后一个徽章值,并发送它.

转载注明原文:在ios7中更新推送通知的徽章计数 - 代码日志