user2230971
user2230971

Reputation: 287

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;
   }
}

Upvotes: -1

Views: 8558

Answers (3)

Sanjay Bharwani
Sanjay Bharwani

Reputation: 4809

For us the solution was to set mutableContent as true in Message

Message.builder().setApnsConfig(
ApnsConfig.builder()
.setAps(Aps.builder().setMutableContent(true).build())
.build())

And the incremental of the basgeCount is handled on the app side as part of the extension. Below is the snippet

// Increment badge count
        let badgeCountKey = "NotificationBadgeCount"
        let userDefaults = UserDefaults(suiteName: "group.com.app.iphone.demo")
        let badgeCount = userDefaults?.value(forKey: badgeCountKey) as? Int
        if (badgeCount != nil && badgeCount! > 0) {
            bestAttemptContent?.badge = NSNumber(value: Int32(badgeCount! + 1))
            userDefaults?.set(badgeCount! + 1, forKey: badgeCountKey)
        } else {
            bestAttemptContent?.badge = 1
            userDefaults?.set(1, forKey: badgeCountKey)
        }

Upvotes: 0

souvickcse
souvickcse

Reputation: 7804

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.

Upvotes: 2

Eran
Eran

Reputation: 394016

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.

Upvotes: 1

Related Questions