iOS Push Notifications - update badge without alert?

18,045

Solution 1

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.

Solution 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

Solution 3

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

{

    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
}

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

Solution 4

You can use

[UIApplication sharedApplication].applicationIconBadgeNumber = aNumber;
Share:
18,045
Ran Dahan
Author by

Ran Dahan

Updated on June 03, 2022

Comments

  • Ran Dahan
    Ran Dahan almost 2 years

    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
    Ran Dahan about 11 years
    I'm sorry I wasn't clear, I meant when the app is closed. See my edit.
  • Ran Dahan
    Ran Dahan about 11 years
    I'm sorry I wasn't clear, I meant when the app is closed. See my edit.
  • nsgulliver
    nsgulliver about 11 years
    In that case you will have method didReceiveRemoteNotification and you can do whatever you want within this method
  • Ran Dahan
    Ran Dahan about 11 years
    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.
  • nsgulliver
    nsgulliver about 11 years
    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
  • Ran Dahan
    Ran Dahan about 11 years
    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.
  • nsgulliver
    nsgulliver about 11 years
    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
    nsgulliver about 11 years
    yes, you should read carefully how to register for remote notifications it is called APN Service
  • Trj
    Trj about 11 years
    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...
  • siva
    siva almost 10 years
    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".
  • Eran
    Eran almost 10 years
    @siva Of course it's possible. The badge parameter is optional.
  • Raptor
    Raptor over 9 years
    UPDATE: In iOS 8 and later, the maximum size allowed for a notification payload is 2 kilobytes
  • makerofthings7
    makerofthings7 about 8 years
    @Raptor it's 2K if using the binary method, 4K if using HTTP/2
  • Lal Krishna
    Lal Krishna about 6 years
    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)