15

Is there a way to update the number in the badge without showing an alert or opening the app?

I am writing an app that should always display the current number of unread messages in the icon badge, but I want to do so without displaying any alerts to the user.

I am developing for iOS 5.0+.

EDIT: To be more clear, I am asking about a way to do this when the app is not running. I want the server to push a new badge number without showing an alert.. Is this possible?

Ran Dahan
  • 415
  • 1
  • 3
  • 10
  • AFAIK its completely handled by the OS. Do you have any specific reason why you don't want to show the alert? – Anupdas Mar 05 '13 at 14:09
  • The app is build for a very high message frequency and I don't want to bug the user.. So according to what you're saying - I can only achieve this behavior if the user goes to the Notification Center and disables alerts? – Ran Dahan Mar 05 '13 at 14:12
  • @Pandu1251 didn't understand your last sentance.. What logic are you talking about? – Ran Dahan Mar 05 '13 at 14:26
  • @Ran,If you use push notification service, then receiving a push will always show an alert message, and it is the default properties of iOS. If the apps is closed, it will also show the alert message. But you wanted to not show the alert message, wants to Show the badge number. So, you shouldn't use Push Notification, as it will automatically show an alert message. You can do a periodic server request to get the current badge number to show as a local notification. And this code needs to run in background. – sumon Mar 05 '13 at 14:42
  • @sumon I was hoping this was possible similarly to Windows Phone tile notifications... Oh well. Thank you for the answer. – Ran Dahan Mar 05 '13 at 14:43

4 Answers4

27

You can do it. It is possible to send a push notification without an alert. You can even register your application just to badge notifications, in which case the provider server won't even be able to send alerts or sounds.

The Notification Payload

Each push notification carries with it a payload. The payload specifies how users are to be alerted to the data waiting to be downloaded to the client application. The maximum size allowed for a notification payload is 256 bytes; Apple Push Notification Service refuses any notification that exceeds this limit. Remember that delivery of notifications is “best effort” and is not guaranteed.

For each notification, providers must compose a JSON dictionary object that strictly adheres to RFC 4627. This dictionary must contain another dictionary identified by the key aps. The aps dictionary contains one or more properties that specify the following actions:

An alert message to display to the user

A number to badge the application icon with

A sound to play

Note that it says one or more of the properties. The alert property is optional. You can even send a notification with an empty aps dictionary (i.e. send only custom properties).

Example 5. The following example shows an empty aps dictionary; because the badge property is missing, any current badge number shown on the application icon is removed. The acme2 custom property is an array of two integers.

{

    "aps" : {

    },

    "acme2" : [ 5,  8 ]

}

The only alert the user will see it the alert that asks him/her whether to allow push notifications. That alert will only be displayed the first time the app is launched after installation.

In this example you register to non alert notifications (badges and sounds only) :

Listing 2-3  Registering for remote notifications

- (void)applicationDidFinishLaunching:(UIApplication *)app {

   // other setup tasks here....

    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];

}



// Delegation methods

- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)devToken {

    const void *devTokenBytes = [devToken bytes];

    self.registered = YES;

    [self sendProviderDeviceToken:devTokenBytes]; // custom method

}



- (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err {

    NSLog(@"Error in registration. Error: %@", err);

}

All quotes are taken from the Apple Local and Push notifications programming guide.

Eran
  • 387,369
  • 54
  • 702
  • 768
  • Eran, i have a follow-up question. If my app is running in the background monitoring user location, can the app receive the notification and execute some code without the user knowing or opening the app? I would want the app to send its location to the server if it receives a push notification... – Trj Mar 08 '13 at 07:54
  • is it possible to send push notification with out badge number in the payload,bcoz i don't want to use badge concept. or shall i set to badge number "0". – siva Jun 04 '14 at 11:15
  • @siva Of course it's possible. The badge parameter is optional. – Eran Jun 04 '14 at 14:45
  • **UPDATE**: In iOS 8 and later, the maximum size allowed for a notification payload is **2 kilobytes** – Raptor Jan 01 '15 at 06:32
  • 1
    @Raptor it's 2K if using the binary method, 4K if using HTTP/2 – makerofthings7 Apr 05 '16 at 11:29
  • **UPDATE**: For regular remote notifications, the maximum size is 4KB (4096 bytes)(2KB in Binary interface), For Voice over Internet Protocol (VoIP) notifications, the maximum size is 5KB (5120 bytes) – Lal Krishna Apr 19 '18 at 11:26
2

you should use applicationIconBadgeNumber for locally handling your app badge number

[UIApplication sharedApplication].applicationIconBadgeNumber = number_of_notifications;

I don't think it is possible to do without alert as far as adding badge counter from remote notification. You should read about APN Service, in your case you might register for UIRemoteNotificationTypeBadge you should read about Local & Push Notification Programming guide

nsgulliver
  • 12,655
  • 23
  • 43
  • 64
  • I'm sorry I wasn't clear, I meant when the app is closed. See my edit. – Ran Dahan Mar 05 '13 at 14:06
  • In that case you will have method didReceiveRemoteNotification and you can do whatever you want within this method – nsgulliver Mar 05 '13 at 14:08
  • I want to update the badge without the user running the app, it has no value to me if the user already opened the app. Kind of like the iOS built-in mail app. – Ran Dahan Mar 05 '13 at 14:09
  • you will register for that particular notification, for example in your case it might be `UIRemoteNotificationTypeBadge`, you should read carefully how remote notifications are handled – nsgulliver Mar 05 '13 at 14:12
  • Maybe I'm missing something, but doesn't `didReceiveRemoteNotification` execute when the app launches? I don't want to launch the app, only update the badge without any app code running. – Ran Dahan Mar 05 '13 at 14:28
  • Exactly, didReceiveRemoteNotification is a delegate method of AppDelegate and it runs when app runs, stuff you are talking about is handled by OS and you register your app for the notification on the itunesConnect – nsgulliver Mar 05 '13 at 14:32
  • yes, you should read carefully how to register for remote notifications it is called [APN Service](http://developer.apple.com/library/ios/#documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/IPhoneOSClientImp/IPhoneOSClientImp.html) – nsgulliver Mar 05 '13 at 14:34
1

You can use

[UIApplication sharedApplication].applicationIconBadgeNumber = aNumber;
Samet DEDE
  • 1,621
  • 19
  • 28
1
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo

{

    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
}

use this method....this will help u.

nsgulliver
  • 12,655
  • 23
  • 43
  • 64
Hari1251
  • 452
  • 4
  • 15