Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I want to warn against calling </p> <pre><code>[[UIApplication sharedApplication] setApplicationIconBadgeNumber:bg_NUM] </code></pre> <p>from</p> <pre><code>- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification </code></pre> <p>method!</p> <p>If your scheduled local notification with some badge number, then badge will be set <strong>asynchronously</strong> some milliseconds <strong>after</strong> enter to -didReceiveLocalNotification</p> <p>for sample:</p> <pre><code>-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification { [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0]; // ^^^ maybe not reset badge to 0!! ^^^ } </code></pre> <p>another code:</p> <pre><code>-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification { sleep(1); //waiting for system is set our scheduled badge [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0]; // ^^^ most chances for reset badge to 0 ^^^ } </code></pre> <p><strong>Testing code, screen touches schedule local notifications and calculate delay before system real set badge:</strong></p> <pre><code>- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0]; UILocalNotification *localNotif = [[[UILocalNotification alloc] init] autorelease]; localNotif.applicationIconBadgeNumber = rand()%100+1; localNotif.fireDate = [NSDate dateWithTimeIntervalSinceNow:1]; [[UIApplication sharedApplication] scheduleLocalNotification:localNotif]; } [...] - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification { CFAbsoluteTime startTime = CFAbsoluteTimeGetCurrent(); while ([UIApplication sharedApplication].applicationIconBadgeNumber == 0) sleep(0); NSLog(@"badge set: %d after %f sec.", [UIApplication sharedApplication].applicationIconBadgeNumber, CFAbsoluteTimeGetCurrent()-startTime); } </code></pre> <p>output:</p> <pre><code>badge set: 41 after 0.000839 sec. badge set: 9 after 0.000754 sec. badge set: 56 after 0.076026 sec. badge set: 17 after 0.069889 sec. badge set: 8 after 0.056245 sec. badge set: 71 after 0.120729 sec. badge set: 28 after 0.122720 sec. badge set: 17 after 0.000758 sec. </code></pre> <p>this testing in iOS 4.2/4.3/5.0/6.1 on different devices</p> <p><em><strong>Be careful when you reset badge number in -didReceiveLocalNotification message!</em></strong> (this true only for LocalNotification /not remote push/ and only if application is active on receive moment)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload