对于推送通知,徽章计数没有增加。总计徽章数仍为1?

时间:2013-09-24 12:42:35

标签: ios xcode push-notification apple-push-notifications badge

当应用处于推送通知的后台时,我的应用徽章数量不会增加。仅第一次推送通知的数量增加1,并且始终将徽章计数保留为1,如果我获得超过1次通知,则徽章计数仅保留1次。 以下是我的代码

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

    NSString *message = nil;
    id alert = [userInfo objectForKey:@"aps"];
    if ([alert isKindOfClass:[NSString class]]) {
        message = alert;
    }    
    else if ([alert isKindOfClass:[NSDictionary class]]) {
        message = [alert objectForKey:@"alert"];
    }
    if (alert) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"xyz"
                                                        message:message
                                                       delegate:self
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:@"Cancel", nil];
        alertView.tag=2525;
        [alertView show];
     }
}


-(void)alertView:(UIAlertView *)alertView 
     clickedButtonAtIndex:(NSInteger)buttonIndex  {

   if(alertView.tag==2525)  {
      [UIApplication sharedApplication].applicationIconBadgeNumber =
      [UIApplication sharedApplication].applicationIconBadgeNumber-1;
   }
}

2 个答案:

答案 0 :(得分:2)

您必须从服务器端执行此操作。在我的情况下,我通过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

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

答案 1 :(得分:1)

你说你的有效载荷是:

aps = { alert = "third testing"; badge = 1; sound = "sound.caf"; };

由于您始终发送1徽章计数,因此这是为您的应用显示的徽章计数。徽章计数不是增量的。如果要查看徽章计数高于1,则服务器应在有效负载中设置大于1的值。

您的服务器应该跟踪每个设备应该接收的徽章数。应用程序本身无法保证接收所有推送通知,因此您无法依赖其逻辑来更新徽章计数。

相关问题