解决方法
>试试吧.它已弃用但工作代码.在iOS 10.0之前使用它:
//Get all prevIoUs noti.. NSLog(@"scheduled notifications: --%@----",[[UIApplication sharedApplication] scheduledlocalnotifications]); NSDate *Now = [NSDate date]; Now = [Now dateByAddingTimeInterval:60*60*24*7]; //7 for 7th day of the week. NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; [calendar setTimeZone:[NSTimeZone localTimeZone]]; NSDateComponents *components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit|NSTimeZoneCalendarUnit fromDate:Now]; NSDate *SetAlarmAt = [calendar dateFromComponents:components]; UIlocalnotification *localnotification = [[UIlocalnotification alloc] init]; localnotification.fireDate = SetAlarmAt; NSLog(@"FIRE DATE --%@----",[SetAlarmAt description]); localnotification.alertBody =@"Alert"; localnotification.alertAction = [Nsstring stringWithFormat:@"My test for Weekly alarm"]; localnotification.userInfo = @{ @"alarmID":[Nsstring stringWithFormat:@"123"],@"SOUND_TYPE":[Nsstring stringWithFormat:@"hello.mp3"] }; localnotification.repeatInterval=0; //[NSCalendar currentCalendar]; [[UIApplication sharedApplication] schedulelocalnotification:localnotification];
>对于iOS 10.0及更高版本:现在尝试使用UserNotifications框架:添加框架,并像#import一样导入.在Appdelegate Didfinishluanch方法.
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; [center requestAuthorizationWithOptions:(UNAuthorizationoptionBadge | UNAuthorizationoptionSound | UNAuthorizationoptionAlert) completionHandler:^(BOOL granted,NSError * _Nullable error) { if (!error) { NSLog(@"request succeeded!"); [self testAlrt]; } }];
在你的ibaction或方法中,写下它并测试:
NSDate *Now = [NSDate date]; // NSLog(@"NSDate--before:%@",Now); Now = [Now dateByAddingTimeInterval:60*60*24*7]; NSLog(@"NSDate:%@",Now); NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; [calendar setTimeZone:[NSTimeZone localTimeZone]]; NSDateComponents *components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit|NSTimeZoneCalendarUnit fromDate:Now]; NSDate *todaySehri = [calendar dateFromComponents:components]; //unused UNMutableNotificationContent *objNotificationContent = [[UNMutableNotificationContent alloc] init]; objNotificationContent.title = [Nsstring localizedUserNotificationStringForKey:@"Notification!" arguments:nil]; objNotificationContent.body = [Nsstring localizedUserNotificationStringForKey:@"This is local notification message!" 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:@"ten" 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"); } }];
原文地址:https://www.jb51.cc/iOS/335382.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。