ViewController.m
- (IBAction)setAlarm:(id)sender {
[defaults setBool:YES forKey:@"notificationIsActive"];
[defaults synchronize];
//self.message.text=@"Notifications Started";
NSTimeInterval interval;
interval = 10;
NSLog(@"time interval.....%f",interval);
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:interval]; //Enter the time here in seconds.
localNotification.alertBody= @"This is message Users will see";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.repeatInterval= NSCalendarUnitDay;//NSCalendarUnitMinute;
//Repeating instructions here.
localNotification.soundName= UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}
我通过按钮点击将此代码用于通知警报。此代码可与我的应用程序一起使用多次,以提供通知警报。但突然它停止工作。请帮忙。
答案 0 :(得分:1)
如果您使用的是iOS 10&上面,Apple Doc说
在iOS 10中不推荐使用
UILocalNotification
。使用UNNotificationRequest
代替。UILocalNotification
对象指定一个通知 应用程序可以安排在特定日期和时间进行演示。
参考链接:
答案 1 :(得分:0)
您需要在iOS 10及更高版本中使用它。
UNMutableNotificationContent *content = [UNMutableNotificationContent new];
content.title = @"Don't forget";
content.body = @"Buy some milk";
content.sound = [UNNotificationSound defaultSound];
在某个时间
NSDateComponents* date = [[NSDateComponents alloc] init];
date.hour = 7;
date.minute = 0;
UNCalendarNotificationTrigger* trigger = [UNCalendarNotificationTrigger
triggerWithDateMatchingComponents:date repeats:NO];
UNNotificationRequest* request = [UNNotificationRequest requestWithIdentifier:@"MorningAlarm" content:content trigger:trigger];