生成本地通知每28天iOS 10 Objective-C

我必须每28天产生一个本地通知,即用户将在特定的date注册,我需要在注册date28天之后生成一个本地通知,然后我需要重复生成每个本地通知28天。

从这里检查接受的答案: 如何在iOS 10中安排本地通知(objective-c)

  1. 尝试一下。 它不赞成,但工作代码。 在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]; 
    1. 对于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或方法中,写下来并testing:

  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"); } }];