我已经实现了iOS 10本地通知,它适用于第一次闹钟,但不适用于其他闹钟。我已经导入了iOS 10库,实现了委托,在委托中第一次接收它,但它不会稍后触发。iOS本地通知不是第二次触发,而是在取消通知请求中显示
我的日期,甲酸是这样的:2017年5月28日14时04分07秒+0000
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
[calendar setTimeZone:[NSTimeZone localTimeZone]];
//(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute)
NSDateComponents *components = [calendar components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute) fromDate:now];
NSDate *todaySehri = [calendar dateFromComponents:components];
UNMutableNotificationContent *objNotificationContent = [[UNMutableNotificationContent alloc] init];
objNotificationContent.title = [NSString localizedUserNotificationStringForKey:[NSString stringWithFormat:@"Local.."]] arguments:nil];
objNotificationContent.body = [NSString localizedUserNotificationStringForKey:@"Incoming Local Notification" arguments:nil];
objNotificationContent.sound = [UNNotificationSound soundNamed:[NSString stringWithFormat:@"%@",soundFileBtn.titleLabel.text]]; // [UNNotificationSound defaultSound];//soundFileBtn.titleLabel.text;// [UNNotificationSound defaultSound];
NSDictionary *dict = @{@"alarmID":self.myKey,
@"SOUND_TYPE":[NSString stringWithFormat:@"%li",(long)self.audio_segment.selectedSegmentIndex]
};
NSArray *array = [NSArray arrayWithObjects:dict, nil];
NSDictionary *data = [NSDictionary dictionaryWithObject:array forKey:@"payload"];
[objNotificationContent setUserInfo:data];
//update application icon badge number
objNotificationContent.badge = @([[UIApplication sharedApplication] applicationIconBadgeNumber] + 0);
NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *compss= [cal components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute)
fromDate:todaySehri];
UNCalendarNotificationTrigger *trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:compss repeats:NO];
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:[todaySehri description]
content:objNotificationContent trigger:trigger];
//schedule localNotification
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
if (!error) {
NSLog(@"Local Notification succeeded");
}
else {
NSLog(@"Local Notification failed");
}
}];
尝试打印您为每个触发器设置的'identifier',以检查它们是否不同。另外你如何测试通知?通过手动更改设备上的时间或等待时间? – bhakti123
标识符在我的NSLog中不同。即使我附加一个额外的随机字符串到我的标识符。我正在模拟器上检查报警。我设置了3个独立的警报。我设置我的MacBook时间接近第一次报警。当它工作,然后我再次改变我的MacBook时间接近第二次报警并运行我的项目。但它不适用于第二次或其他警报。 @ bhakti123 –
检查是否所有的警报都是预定的。他们的下一个触发日期是什么。然后尝试根据该触发日期更改时间并查看它是否有效。 – bhakti123