从iOS9开始,本地通知无法正常运行。
有时用户会收到通知,有时则不会。我的通知每天重复一次。
知道是什么原因引起的问题吗?我看到了一些帖子,说明iOS9中存在一个错误,但是我不确定这就是原因。
这是一段代码:
NSDate *alarmDate = [date dateByAddingTimeInterval:DEFAULT_SNOOZE_DURATION * i];
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
localNotif.fireDate = alarmDate;
localNotif.timeZone = nil;
localNotif.alertBody = alertBody;
localNotif.hasAction = YES;
localNotif.applicationIconBadgeNumber = 1;
localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.repeatInterval = NSCalendarUnitDay;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif]
谢谢你的帮助
最佳答案
在AppDelegate.m中编写以下代码
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
[launchOptions valueForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)])
{
[application registerUserNotificationSettings:[UIUserNotificationSettings
settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|
UIUserNotificationTypeSound categories:nil]];
}
return YES;
}
还要在AppDeleagte.m中编写此代码
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
UIAlertView *notificationAlert = [[UIAlertView alloc] initWithTitle:@"Reminder" message:notification.alertBody
delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[notificationAlert show];
// Set icon badge number to zero
application.applicationIconBadgeNumber = 0;
}
在您要实现本地通知的ViewController中编写以下代码。
例如:在这里我初始化UILocalNotification,它的触发日期是currentTime和5seconds间隔时间。
触发fire localNotification后,它将在您的屏幕上发出警报。
- (void)viewDidLoad
{
[super viewDidLoad];
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:5];
notification.repeatInterval = NSDayCalendarUnit;
notification.alertBody = @"This is local notification!";
notification.timeZone = [NSTimeZone defaultTimeZone];
notification.soundName = UILocalNotificationDefaultSoundName;
notification.applicationIconBadgeNumber = 10;
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
关于ios9 - iOS9的UILocalNotification问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32901918/