【发布时间】:2017-10-23 07:01:31
【问题描述】:
我已经实现了 iOS 10 本地通知,它适用于第一个警报,但不适用于其他警报。我已经导入 iOS 10 库,实现了委托,第一次在委托中接收它,但它只是稍后没有触发。
我的日期格式如下:2017-05-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,以检查它们是否不同。另外,您如何测试通知?通过手动更改设备上的时间或等待时间? -
标识符在我的 NSLog 中不同。即使我在我的标识符中附加了一个额外的随机字符串。我正在检查模拟器上的警报。我设置了3个单独的闹钟。我将我的 macbook 时间设置为接近第一个闹钟。当它工作时,我再次将我的 macbook 时间更改为接近第二个闹钟并运行我的项目。但它不适用于第二个或其他警报。 @bhakti123
-
检查是否所有的警报都安排好了。他们的下一个触发日期是什么时候。然后尝试根据该触发日期更改时间,看看是否有效。
-
预定是什么意思?我怎么查?我的 compss 包含我设置触发的警报的触发点。 @bhakti123
-
您可以使用
getpendingnotificationrequests查看定时闹钟
标签: objective-c xcode ios10 local unusernotificationcenter