通知提醒无效

时间:2017-05-24 09:59:47

标签: ios objective-c

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];

}

我通过按钮点击将此代码用于通知警报。此代码可与我的应用程序一起使用多次,以提供通知警报。但突然它停止工作。请帮忙。

2 个答案:

答案 0 :(得分:1)

如果您使用的是iOS 10&上面,Apple Doc说

  在iOS 10中不推荐使用

UILocalNotification。使用UNNotificationRequest   代替。 UILocalNotification对象指定一个通知   应用程序可以安排在特定日期和时间进行演示。

参考链接:

UserNotifications

Local Notification in iOS 10 for Objective-C and Swift 3

答案 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];