iOS Push Notifications - update badge without alert?
Asked Answered
M

4

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?

Margarite answered 5/3, 2013 at 13:59 Comment(5)
AFAIK its completely handled by the OS. Do you have any specific reason why you don't want to show the alert?Lamberto
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?Margarite
@Pandu1251 didn't understand your last sentance.. What logic are you talking about?Margarite
@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.Cerenkov
@Cerenkov I was hoping this was possible similarly to Windows Phone tile notifications... Oh well. Thank you for the answer.Margarite
T
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.

Tabanid answered 5/3, 2013 at 15:31 Comment(6)
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...Engeddi
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".Pronate
@Pronate Of course it's possible. The badge parameter is optional.Tabanid
UPDATE: In iOS 8 and later, the maximum size allowed for a notification payload is 2 kilobytesLegend
@Legend it's 2K if using the binary method, 4K if using HTTP/2Aegeus
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)Bromoform
D
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

Disentwine answered 5/3, 2013 at 14:4 Comment(7)
I'm sorry I wasn't clear, I meant when the app is closed. See my edit.Margarite
In that case you will have method didReceiveRemoteNotification and you can do whatever you want within this methodDisentwine
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.Margarite
you will register for that particular notification, for example in your case it might be UIRemoteNotificationTypeBadge, you should read carefully how remote notifications are handledDisentwine
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.Margarite
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 itunesConnectDisentwine
yes, you should read carefully how to register for remote notifications it is called APN ServiceDisentwine
P
1

You can use

[UIApplication sharedApplication].applicationIconBadgeNumber = aNumber;
Pegboard answered 5/3, 2013 at 14:3 Comment(1)
I'm sorry I wasn't clear, I meant when the app is closed. See my edit.Margarite
B
1
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo

{

    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
}

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

Bluegreen answered 5/3, 2013 at 14:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.