我已经为本地通知构建了一个测试应用程序。看起来像这样
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:10];
localNotification.alertBody = @"My Next Custom Notification";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:15];
localNotification.alertBody = @"Second One Now";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
NSLog(@"Notifications: %@", [[UIApplication sharedApplication] scheduledLocalNotifications]);
我的AppDelegate:
- (void)applicationWillEnterForeground:(UIApplication *)application
{
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
NSLog(@"%s", __PRETTY_FUNCTION__);
}
当我在后台或LockScreen中收到此通知时,它可以正常工作。 但是当第一个通知进来时,它并没有显示应该是" 1"现在。 Wehen第二个通知出现在徽章显示" 1"而不是" 2"(现在是正确的数字)。所以我的代码有什么问题。 任何建议都很明显......
答案 0 :(得分:1)
您正在制定一个简单的错误,即您首先安排通知而不是设置其徽章编号。因此,您应首先设置通知徽章编号,然后安排它。
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:10];
localNotification.alertBody = @"My Next Custom Notification";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:15];
localNotification.alertBody = @"Second One Now";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.applicationIconBadgeNumber = localNotification.applicationIconBadgeNumber + 1;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
NSLog(@"Notifications: %@", [[UIApplication sharedApplication] scheduledLocalNotifications]);
同样,对于第二次通知期间的右侧徽章编号,您应该保存通知徽章编号,或者像上面的代码中那样在上一个通知徽章中设置。因为在您的代码中,它只是从当前的[UIApplication sharedApplication]徽章设置徽章编号,每次0 + 1等于1。
答案 1 :(得分:0)
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:10];
localNotification.alertBody = @"My Next Custom Notification";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
localNotification.applicationIconBadgeNumber = 1; //first notification
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:15];
localNotification.alertBody = @"Second One Now";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
localNotification.applicationIconBadgeNumber = 2; // second notification
NSLog(@"Notifications: %@", [[UIApplication sharedApplication] scheduledLocalNotifications]);
添加条件检查后,首先是火,然后是cancelallnotification
并注册剩余的徽章顺序递减。