IOS10.3本地通知目标-c
我想在IOS 10.3中使用objective-c为某个日期选择器创建一个本地通知,但由于某些原因它无法工作。 这是我的密码IOS10.3本地通知目标-c,ios,objective-c,localnotification,Ios,Objective C,Localnotification,我想在IOS 10.3中使用objective-c为某个日期选择器创建一个本地通知,但由于某些原因它无法工作。 这是我的密码 -(IBAction)addReminder:(id)sender { NSDate *date =self.datePicker.date; UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc]init]; content.title = [N
-(IBAction)addReminder:(id)sender
{
NSDate *date =self.datePicker.date;
UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc]init];
content.title = [NSString localizedUserNotificationStringForKey:@"wake up" arguments:nil];
content.body = [NSString localizedUserNotificationStringForKey:@"it is time" arguments:nil];
NSDateComponents *dateComponent = [[NSDateComponents alloc]init];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *Components = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute fromDate:date];
dateComponent.hour = [Components hour];
dateComponent.minute = [Components minute];
UNCalendarNotificationTrigger *trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:dateComponent repeats:NO];
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"Alarm" content:content trigger:trigger];
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
// Ask user for the permission
[center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert + UNAuthorizationOptionSound)
completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (granted)
{
// Add your notification to center
[center addNotificationRequest:request
withCompletionHandler:^(NSError * _Nullable error) {
if (error != nil)
{
NSLog(@"%@", [error localizedDescription]);
}
NSLog(@"finished scheduling");
}];
}
else
{
NSLog(@"%i",granted);
}
}];
}
在我运行代码并选择某个日期之后。通知不起作用,我在控制台中为“已授予变量”获取了0。第一次收到请求时您是否授予了权限?若你们在“系统设置”应用程序中打开你们的应用程序,会发生什么?那个里是否启用了通知?我实际上是在模拟器上测试它。在模拟器上运行它之前,我需要授予它权限吗。谢谢,你总是需要给予许可。谢谢你的帮助。如果你不介意的话,我想问你一个简单的问题。我该在哪里准许呢。它是否在AppDelegate文件中?这是通常的位置,但这并不重要-您这样做应该没问题。重要的部分是在系统显示警报时按下“允许”。当您首次请求授权时,每次安装只会发生一次。在此之后,您可以在应用程序中更改此设置,或从设备/模拟器中删除应用程序并重新安装。