我已经设置了每分钟重复一次的本地通知,但是我需要每次都增加应用程序徽章编号。当我运行它的时候它似乎没有增加,它只是保持1.请有人帮助我吗?
以下是我创建通知的方式:
// Create the UILocalNotification
UILocalNotification *myNotification = [[UILocalNotification alloc] init];
myNotification.alertBody = @"Blah blah blah...";
myNotification.alertAction = @"Blah";
myNotification.soundName = UILocalNotificationDefaultSoundName;
myNotification.applicationIconBadgeNumber++;
myNotification.timeZone = [NSTimeZone defaultTimeZone];
myNotification.repeatInterval = NSMinuteCalendarUnit;
myNotification.fireDate = [[NSDate date] dateByAddingTimeInterval:30];
[[UIApplication sharedApplication] scheduleLocalNotification:myNotification];
答案 0 :(得分:21)
经过大量的研究后,我发现解决方案是没有解决方案:
iPhone: Incrementing the application badge through a local notification
当您的应用在后台时,无法使用本地通知动态更新徽章编号。您必须使用推送通知。
答案 1 :(得分:1)
如果您使用Parse for Push等外部服务,则应该可以轻松完成。只需在触发本地通知时递增Parses徽章编号。虽然,这是一个特例。
答案 2 :(得分:0)
虽然没有简单的applicationIconBadgeNumber++
方法,正如BFar所提到的,每当添加或删除通知时,您都可以通过更新所有已调度的UILocalNotifications的applicationIconBadgeNumbers来实现您的要求。
虽然只要您在正确的时间拨打repeatInterval
和scheduleNotification
,但如果您的通知使用decrementBadgeNumber
,,则无效下面的课应该做的伎俩。
@implementation NotificationScheduler
+ (void) scheduleNotification:(NSString*)message date:(NSDate*)date {
UIApplication *app = [UIApplication sharedApplication];
UILocalNotification *notification = [[UILocalNotification alloc] init];
if (notification) {
notification.fireDate = date;
notification.timeZone = [NSTimeZone defaultTimeZone];
notification.alertBody = message;
notification.soundName = UILocalNotificationDefaultSoundName;
notification.applicationIconBadgeNumber = [self getExpectedApplicationIconBadgeNumber:date];
[app scheduleLocalNotification:notification];
[self updateBadgeCountsForQueuedNotifiations];
}
}
+ (void) decrementBadgeNumber:(long)amount {
[self setCurrentBadgeNumber:([self getCurrentBadgeNumber] - amount)];
[self updateBadgeCountsForQueuedNotifiations];
}
+ (long) getExpectedApplicationIconBadgeNumber:(NSDate*)notificationDate {
long number = [self getCurrentBadgeNumber];
for (UILocalNotification *notice in [self getScheduledLocalNotifications]) {
if (notice.fireDate <= notificationDate) {
number++;
}
}
return number;
}
+ (void) updateBadgeCountsForScheduledNotifiations {
long expectedBadgeNumber = [self getCurrentBadgeNumber];
NSArray *allLocalNotifications = [self getScheduledLocalNotifications];
for (UILocalNotification *notice in allLocalNotifications) {
expectedBadgeNumber++;
notice.applicationIconBadgeNumber = expectedBadgeNumber;
}
[[UIApplication sharedApplication] setScheduledLocalNotifications:allLocalNotifications];
}
+ (long) getCurrentBadgeNumber {
return [UIApplication sharedApplication].applicationIconBadgeNumber;
}
+ (void) setCurrentBadgeNumber:(long)number {
[UIApplication sharedApplication].applicationIconBadgeNumber = number;
}
+ (NSArray*) getScheduledLocalNotifications {
NSSortDescriptor * fireDateDesc = [NSSortDescriptor sortDescriptorWithKey:@"fireDate" ascending:YES];
return [[[UIApplication sharedApplication] scheduledLocalNotifications] sortedArrayUsingDescriptors:@[fireDateDesc]];
}
@end
答案 3 :(得分:0)
我可以在安排本地通知时使用以下行进行操作
localNotification.applicationIconBadgeNumber = [UIApplication sharedApplication].applicationIconBadgeNumber + 1;
,另一端是appdelegate
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
application.applicationIconBadgeNumber -= 1;
}
答案 4 :(得分:-1)
尝试类似:
int plusOne = [myNotification.applicationIconBadgeNumber intValue];
plusOne++;
myNotification.applicationIconBadgeNumber = plusOne;
答案 5 :(得分:-1)
这应该有用。
myNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
答案 6 :(得分:-1)
试试这个...它在简单的场景中对我有用......
notification.applicationIconBadgeNumber = [UIApplication sharedApplication].scheduledLocalNotifications.count + 1;
并且不要忘记在应用启动时将徽章图标设置回0。