徽章计数没有增加推送通知。总是徽章计数保持1?

当应用程序处于后台进行推送通知时,我的应用程序徽章数量不会增加。仅当第一次推送通知时计数增加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; } } 

你必须从服务器端来完成。 在我的情况下,我已经通过PHP和MySQL做到了。 这是我的数据库 在这里输入图像说明

我已经添加了一个字段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 

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

你说你的有效载荷是:

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

由于您总是发送1徽章计数,这就是为您的应用程序显示的徽章数量。 徽章数不是增量式的。 如果要查看徽章数量高于1,则服务器应将有效负载中的值设置为大于1。

您的服务器应该跟踪每个设备应该接收哪个徽章。 应用程序本身并不能保证接收到所有的推送通知,所以你不能依靠它的逻辑来更新徽章计数。