-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;
   }
}
Eran
  • 387,369
  • 54
  • 702
  • 768
user2230971
  • 287
  • 1
  • 7
  • 22
  • I'm seeing a lot of similar questions concerning push notifications and certs, just wondering what class this is from. – zaph Sep 24 '13 at 12:45
  • What payload does your server send to APNS? – Moxy Sep 24 '13 at 12:53
  • [[UIApplication sharedApplication] registerForRemoteNotificationTypes: (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)]; i am using above payloads – user2230971 Sep 24 '13 at 13:04
  • I meant the data that your sever is sending, what value does it set for badge? – Moxy Sep 24 '13 at 13:15
  • aps = { alert = "third testing"; badge = 1; sound = "sound.caf"; };every time i get this reseponse form server.but i am getting this after clicking on notification – user2230971 Sep 24 '13 at 13:25

2 Answers2

2

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.

souvickcse
  • 7,742
  • 5
  • 37
  • 64
1

You said your payload is :

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

Since you always send 1 for badge count, that's the badge count being displayed for your app. The badge count is not incremental. If you want to see badge counts higher than 1, your server should put values higher than 1 in your payload.

Your server should keep track of which badge count each device should receive. The app itself is not guaranteed to receive all the push notifications, so you can't rely on its logic to update the badge count.

Eran
  • 387,369
  • 54
  • 702
  • 768
  • [UIApplication sharedApplication].applicationIconBadgeNumber =[UIApplication sharedApplication].applicationIconBadgeNumber+1; i am able to increase the badge count by this line but. here what is the problem is iam getting badge count after select the notification i need the get this without selecting notification – user2230971 Sep 24 '13 at 14:36
  • That's another reason you shouldn't increase the badge count programmatically in your app - your code is executed only after the user taps the notification. Setting the badge in the app should only be used to clear it (set it to zero) after the user views the notification. – Eran Sep 24 '13 at 14:44
  • @user2230971 As I suggested in my answer, send a badge > 1 from your server. Let your server maintain a badge count for each device, and decide what badge count to send in the push notification. – Eran Sep 24 '13 at 15:23
  • @user2230971 Your device can send a request to your server that reports (for example) the number of read notifications. – Eran Sep 25 '13 at 14:17
  • You need to send something to your server when read a notification – EnriMR Aug 29 '14 at 18:12