-2

I am creating one iOS Application in which I am getting badge number from server, while the application is in foreground, I am updating the badge number in

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

}

but when the application is in background, I have no idea which function I should call.

Rajneesh071
  • 30,846
  • 15
  • 61
  • 74
Devendra Singh
  • 717
  • 1
  • 13
  • 14
  • try this [UIApplication sharedApplication].applicationIconBadgeNumber = 2; any number u want – Shankar BS Feb 25 '14 at 06:41
  • 3
    You can always edit your own Question. No need to ask [same Question](http://stackoverflow.com/q/21981350/1603072) again and again. – Bhavin Feb 25 '14 at 06:44

4 Answers4

1

You can send the badge number from your server itself.

By default when u specify the badge number in your APN payload there is no need to set the badge number of your application. If you didn't receive the badge number from server then you cant do it anyway until the user open the application.

But in ios7 APN had a new feature.

By setting the flag Content-Available:1 will let your application run immediately after received the notification,there you can set your badge number.

CoolMonster
  • 2,258
  • 25
  • 50
1

You may send badge number in push notification like this

{"aps":{"badge":"3","alert":"help","sound":"sound.caf"}}
nerowolfe
  • 4,787
  • 3
  • 20
  • 19
1

When the application is in the background didReceiveRemoteNotification method never calls. For doing something when your App is in background you need to implement your logic in AppDelegate's applicationDidEnterBackground: method like.

-(void)applicationDidEnterBackground:(UIApplication *)application
{
    [UIApplication sharedApplication].applicationIconBadgeNumber = 2;
}
Tarek Hallak
  • 18,422
  • 7
  • 59
  • 68
Irfan
  • 4,301
  • 6
  • 29
  • 46
  • I know, When the application is in the background didReceiveRemoteNotification method never calls. This method applicationDidEnterBackground: calls when i click on Home button. This is wrong. Badge number never update when application is in background, by this code i can update badge number on clicking home button. This is not my question. I want to update badge number when application is in background. Any confusition?? – Devendra Singh Feb 25 '14 at 10:49
  • 2
    then you can use this method applicationWillEnterForeground,basically its the server responsibility to send you the badge number,as a iOS developer we only clear the badge number. Kindly concern with your web side developer to send you the badge number in push dictionary like // Create the payload body $body['aps'] = array( 'alert' => $message, 'badge' => 1, 'sound' => 'default' ); otherwise you will unable to do that. hopefully this will resolve your confusion. – Irfan Feb 25 '14 at 11:06
0

Use this

- (void)applicationDidEnterBackground:(UIApplication *)application
{

   [UIApplication sharedApplication].applicationIconBadgeNumber = X;
  NSLog(@"Application Did Enter Background");
}

Send you badge number in the payload of push notification

Gajendra Rawat
  • 3,673
  • 2
  • 19
  • 36
Himanshu Joshi
  • 3,391
  • 1
  • 19
  • 32
  • I know, When the application is in the background didReceiveRemoteNotification method never calls. This method applicationDidEnterBackground: calls when i click on Home button. This is wrong. Badge number never update when application is in background, by this code i can update badge number on clicking home button. This is not my question. I want to update badge number when application is in background. Any confusition?? – Devendra Singh Feb 25 '14 at 10:53
  • See this https://developer.apple.com/library/ios/documentation/iphone/conceptual/iphoneosprogrammingguide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html and tell me which state are u saying about? – Himanshu Joshi Feb 25 '14 at 11:17