在iOS 10中安排多个本地通知不起作用
我正在为iOS 10和Xcode 8 Beta 2使用UNUsernotification 我在iOS设备中编写了以下本地通知代码:在iOS 10中安排多个本地通知不起作用,ios,objective-c,iphone,unusernotificationcenter,Ios,Objective C,Iphone,Unusernotificationcenter,我正在为iOS 10和Xcode 8 Beta 2使用UNUsernotification 我在iOS设备中编写了以下本地通知代码: -(void) localNotificationForiOS10:(NSDate *) _reminderDate{ NSLog(@"_reminderDate %@",_reminderDate); NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIden
-(void) localNotificationForiOS10:(NSDate *) _reminderDate{
NSLog(@"_reminderDate %@",_reminderDate);
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
[calendar setTimeZone:[NSTimeZone localTimeZone]];
NSDateComponents *components = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond|NSCalendarUnitTimeZone fromDate:_reminderDate];
NSLog(@"NSDateComponents %@",components);
UNMutableNotificationContent *objNotificationContent = [[UNMutableNotificationContent alloc] init];
objNotificationContent.title = [NSString localizedUserNotificationStringForKey:@"Event Name!" arguments:nil];
objNotificationContent.body = [NSString localizedUserNotificationStringForKey:@"You have event reminder"
arguments:nil];
objNotificationContent.sound = [UNNotificationSound defaultSound];
/// 4. update application icon badge number
objNotificationContent.badge = @([[UIApplication sharedApplication] applicationIconBadgeNumber] + 1);
UNCalendarNotificationTrigger *trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:components repeats:NO];
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"firedate"
content:objNotificationContent trigger:trigger];
/// 3. schedule localNotification
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
if (!error) {
NSLog(@"Local Notification succeeded");
}
else {
NSLog(@"Local Notification failed");
}
}];
}
我想设置三个不同的或多个未来日期,并希望在定义的日期提醒事件。
当我在同一日期的3个不同时间使用上述代码时
例如(2016-12-29 18:052016-12-29 18:102016-12-29 18:15)只有最后一位发出了通知
我在AppDelegate文件中注册位置通知
我试过自己,也试过了 我可以在iOS 10中取消本地通知 这是张贴的代码
每个通知都需要不同的请求标识符,否则只能看到最后一个通知。您正在使用requestWithIdentifier:@“firedate”,因此每个通知的每个请求标识符都使用相同的字符串“firedate”。希望有帮助!好的,我会试试。我有同样的问题,每天的请求标识符都不一样。有什么想法吗?你找到解决办法了吗?
application.applicationIconBadgeNumber = 0;
if ([[[UIDevice currentDevice] systemVersion] floatValue] > 10.0f) {
#if XCODE_VERSION_GREATER_THAN_OR_EQUAL_TO_8
/// schedule localNotification, the delegate must be set before the application returns from applicationDidFinishLaunching:.
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate = self;
#endif
} else {
UILocalNotification *localNotifacation = [self getLocalNotificationFromLaunchOptions:launchOptions];
if (localNotifacation) {
NSString *title = localNotifacation.alertBody;
NSLog(@"Add Title %@",title);
}
}