Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
672 views
in Technique[技术] by (71.8m points)

ios - Badge Count is not increasing for push notification.always badge count remains 1?

My app badge count in not increasing when app is in background for push notifications.Count increase by 1 only for the first push notification and always remains badge count as 1, if i get more then 1 notification also badge count remaing 1 only. Below is my code

- (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;
   }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You have to do it from the server side. In my case I have done it through php and mysql. Here is my database enter image description here

I have added a field badgecount and i increase the badge count every time i send the push to the device with this code

        $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); 
        ...

And I also make another api for making the badgcount 0 which is called in the

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

So when the notification is seen it is again zero in the server.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.8k users

...