简体   繁体   中英

How do you set applicationIconBadgeNumber with scheduleLocalNotification?

I am having some difficulties setting the icon badge with a schedule local notification for my ios application.

I am able to trigger a local notification pop up after 10 seconds when I click the home screen after loading the application. However, the application icon badge number is not getting set. I am using the following code.

UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil) return;
NSDate *fireTime = [[NSDate date] addTimeInterval:10]; // adds 10 secs
localNotif.fireDate = fireTime;
localNotif.alertBody = @"New Message!";
localNotif.applicationIconBadgeNumber = 1;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];

I don't understand why the local notification pops up but the applicationIconBadgeNumber doesn't get set. I am able to set the icon badge number manually by executing the following code instead.

[UIApplication sharedApplication].applicationIconBadgeNumber = 1;

Are you debugging in the Simulator? Same problem on simulator but your code works fine on my iPhone.

are you registering your application for all the badge notification type?

[[UIApplication sharedApplication]registerForRemoteNotificationTypes: UIRemoteNotificationTypeBadge]; 

also, check your notifications settings have not disabled badges.

Here's the code for iOS 8+ in Swift...

let notificationSettings = UIUserNotificationSettings(forTypes: [UIUserNotificationType.Alert , UIUserNotificationType.Badge, UIUserNotificationType.Sound], categories: nil)
application.registerUserNotificationSettings(notificationSettings)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM