2013-09-24 92 views
-1

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

我看到很多关于推送通知和证书的类似问题,只是想知道这是从哪里来的。 – zaph

+0

您的服务器发送给APNS的有效负载是什么? – Moxy

+0

[[UIApplication sharedApplication] registerForRemoteNotificationTypes: (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];我使用的是以上有效载荷 – user2230971

回答

1

你说你的有效载荷是:

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

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

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

+0

[UIApplication sharedApplication] .applicationIconBadgeNumber = [UIApplication sharedApplication] .applicationIconBadgeNumber + 1; 我能够通过这条线增加徽章数量,但是。这里有什么问题是iam在选择通知之后获得徽章数量我需要在没有选择通知的情况下得到这个数字 – user2230971

+0

这是您不应该在应用程序中以编程方式增加徽章数量的另一个原因 - 您的代码仅在用户点击通知后执行。在用户查看通知后,应该只使用应用程序中的徽章来清除它(将其设置为零)。 – Eran

+0

好吧,那么我需要做些什么来增加徽章数量呢? – user2230971

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 

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