在 ios7 中更新推送通知的徽章计数 [英] Update Badge Count For Push Notifications in ios7

查看:22
本文介绍了在 ios7 中更新推送通知的徽章计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正在开发推送通知 应用程序,但徽章计数 在通知出现时没有增加.我在 stack overflow 中看到了很多例子,但没有一个有用.

am working on push notifications app but badge count is not increasing when notification comes. i have saw so many examples in stack overflow but no one is useful.

谁能建议我如何解决这个问题...提前致谢!

Can anyone suggest me how to resolve this problem... thanks in advance!

我的服务器端 PHP 代码:

My Server Side PHP Code:

<?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

in 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;
 }

推荐答案

通常在所有应用程序中,未读通知计数都保存在服务器中.当服务器向特定设备令牌发送推送通知时,服务器会随负载一起发送徽章计数.

Usually in all apps, the unread notification counts are maintained in the server. When the server sends a push notification to a particular device token server sends the badge count along with the payload.

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

Your server logic needs to keep track of the proper badge count and send it appropriately.

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

编辑

您已在 didReceiveRemoteNotification 方法中设置了徽章计数.在通过推送通知设置这个名为 appbadge 的方法之前,因此您必须从服务器设置正确的徽章..

You have set badge count in didReceiveRemoteNotification method. before this method called appbadgeis set from pushnotification, so from server you have to set correct badge..

解决方案:

因此创建一些网络服务,在该网络服务中发送 deviceToken 和 currentBadge 以存储在服务器上,并在下次发送推送时检查令牌的最后一个徽章值,然后发送.

so create some webservice send deviceToken and currentBadge in that webservice to store at server, and when next time you send push check the last badge value for the token, and send it.

这篇关于在 ios7 中更新推送通知的徽章计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆