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

时间:2013-12-04 04:45:40

标签: ios iphone ios7 push-notification apple-push-notifications

正在使用push notifications应用,但badge count在通知到来时没有增加。 我在stack overflow看到了很多例子,但没有人有用。

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

我的服务器端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;
 }

3 个答案:

答案 0 :(得分:4)

通常在所有应用中,未读通知计数都在服务器中维护。 当服务器向特定设备发送推送通知时,令牌服务器会将徽章计数与有效负载一起发送

您的服务器逻辑需要跟踪正确的徽章计数并正确发送。

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

修改

您已在didReceiveRemoteNotification方法中设置徽章计数。在这个名为appbadge的方法是从推送通知设置之前,所以从服务器你必须设置正确的徽章..

解决方案:

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

答案 1 :(得分:1)

当你向其他用户发送通知时,你必须管理你的服务器端,然后从php端设置一个计数器来增加徽章&amp;之后,当您打开应用程序时,将徽章设置为null,将通知发送给其他用户:

- (void) applicationWillResignActive:(UIApplication *) application
{
    application.applicationIconBadgeNumber = 0;
}

&安培;接收您设置此代码的通知:

- (void) application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{    
    NSLog(@"userInfo %@",userInfo);

    for (id key in userInfo)
    {
        NSLog(@"key: %@, value: %@", key, [userInfo objectForKey:key]);
    }

    [application setApplicationIconBadgeNumber:[[[userInfo objectForKey:@"aps"] objectForKey:@"badge"] intValue]];

    NSLog(@"Badge %d",[[[userInfo objectForKey:@"aps"] objectForKey:@"badge"] intValue]);

    NSString *message = [[userInfo objectForKey:@"aps"] objectForKey:@"alert"];

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Test" message:message delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];

    [alert show];
}

答案 2 :(得分:1)

您必须从服务器端执行此操作。在我的情况下,我通过php和mysql完成了它。这是我的数据库 enter image description here

我添加了字段badgecount,每次使用此代码将推送发送到设备时,我都会增加徽章数

    $query = "SELECT badgecount FROM pushnotifications WHERE device_token = '{$device_token}'";
    $query = $this->db->query($query);
    $row = $query->row_array();
    $updatequery = "update pushnotifications set badgecount=badgecount+1 WHERE device_token ='{$device_token}'";
    $updatequery = $this->db->query($updatequery);
    $device = $device_token;
    $payload['aps'] = array('alert' => $pushmessage, 'badge' =>$row["badgecount"]+1, 'sound' => 'default');
    $payload = json_encode($payload); 

我还制作另一个api来制作在

中调用的badgcount 0
- (void) application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo

因此,当看到通知时,它在服务器中再次为零。