我正在开发获取本地通知功能的应用程序。当几个条件匹配时,我需要触发本地通知。我已经完成了当地的通知和行动。但我需要在“通知中心”中显示所有已触发的通知,即使应用程序是前景或背景。
我使用以下代码:
在didFinishLaunchingWithOptions方法中:
if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)]) {
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];
}
当条件很少时匹配:
for (int i = 0; i < [arrayNotif count]; i++) {
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
[localNotif setAlertBody:[NSString stringWithFormat:@"%@", [[arrayNotif objectAtIndex:i] valueForKey:@"title"]]];
[localNotif setFireDate:[NSDate dateWithTimeIntervalSinceNow:1]];
[localNotif setTimeZone:[NSTimeZone defaultTimeZone]];
NSDictionary *dictionary = [NSDictionary dictionaryWithObject:[arrayNotif objectAtIndex:i] forKey:[NSString stringWithFormat:@"%@", [[arrayNotif objectAtIndex:i] valueForKey:@"id"]]];
[localNotif setUserInfo:dictionary];
localNotif.soundName = UILocalNotificationDefaultSoundName;
[localNotif setApplicationIconBadgeNumber:[[UIApplication sharedApplication] applicationIconBadgeNumber] + 1];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
dictionary = nil;
}
我通过 didReceiveLocalNotification: 方法获取所有通知只有应用程序位于前台。现在,我想在通知中心显示所有通知。