首页 文章

如何安排在特定时间过后以某种频率重复的通知

提问于
浏览
0

我没有看到支持安排这种通知,即仅在特定时间过去时重复 . 问题是,虽然重复正确,但计划会立即触发,而不会等待确切的时间 .

例如这是10分钟后安排重复通知的代码 . 这不等待10分钟 - 从现在开始在下一分钟立即开火 .

NSDate *theDate = [NSDate dateWithTimeIntervalSinceNow:60*10];
NSDateComponents *dateForSchedule = [[[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian] components:NSCalendarUnitMinute|NSCalendarUnitHour|NSCalendarUnitDay|NSCalendarUnitWeekday|NSCalendarUnitMonth|NSCalendarUnitYear fromDate:theDate];
UNMutableNotificationContent* content = [[UNMutableNotificationContent alloc] init];
content.body = "Your notification is here.";
content.sound = [UNNotificationSound defaultSound];
content.categoryIdentifier = CALL_NOTIFICATION_CATEGORY;

NSDateComponents* date = [[NSDateComponents alloc] init];

if ([self.selectedFrequecy isEqualToString:HOURLY]) {
    date.minute = dateForSchedule.minute;
} else if ([self.selectedFrequecy isEqualToString:DAILY]) {
    date.hour = dateForSchedule.hour;
    date.minute = dateForSchedule.minute;
} else if ([self.selectedFrequecy isEqualToString:WEEKLY]) {
    date.weekday = dateForSchedule.weekday;
    date.hour = dateForSchedule.hour;
    date.minute = dateForSchedule.minute
} else if ([self.selectedFrequecy isEqualToString:MONTHLY]) {
    date.day = dateForSchedule.day;
    date.hour = dateForSchedule.hour;
    date.minute = dateForSchedule.minute;
} else if ([self.selectedFrequecy isEqualToString:YEARLY] || [self.selectedFrequecy isEqualToString:ONCE]) {
    date.month = dateForSchedule.month;
    date.day = dateForSchedule.day;
    date.hour = dateForSchedule.hour;
    date.minute = dateForSchedule.minute;
}

UNCalendarNotificationTrigger* trigger;

if ([_selectedFrequecy isEqualToString:ONCE]) {
    trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:date repeats:NO];
} else {
    trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:date repeats:YES];
}

// Create the request object.
UNNotificationRequest* request = [UNNotificationRequest requestWithIdentifier:self.callSchedule.id content:content trigger:trigger];

[center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error)
 {
     if (error != nil) {
         NSLog(@"%@", error.localizedDescription)
     }
 }];

这是重复通知的另一个代码块,将在3天后每天触发 .

NSDate *theDate = [NSDate dateWithTimeIntervalSinceNow:60*60*24*3];
NSDateComponents *dateForSchedule = [[[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian] components:NSCalendarUnitMinute|NSCalendarUnitHour|NSCalendarUnitDay|NSCalendarUnitWeekday|NSCalendarUnitMonth|NSCalendarUnitYear fromDate:theDate];
UNMutableNotificationContent* content = [[UNMutableNotificationContent alloc] init];
content.body = "Your notification is here.";
content.sound = [UNNotificationSound defaultSound];
content.categoryIdentifier = CALL_NOTIFICATION_CATEGORY;

NSDateComponents* date = [[NSDateComponents alloc] init];

if ([self.selectedFrequecy isEqualToString:HOURLY]) {
    date.minute = dateForSchedule.minute;
} else if ([self.selectedFrequecy isEqualToString:DAILY]) {
    date.hour = dateForSchedule.hour;
    date.minute = dateForSchedule.minute;
} else if ([self.selectedFrequecy isEqualToString:WEEKLY]) {
    date.weekday = dateForSchedule.weekday;
    date.hour = dateForSchedule.hour;
    date.minute = dateForSchedule.minute
} else if ([self.selectedFrequecy isEqualToString:MONTHLY]) {
    date.day = dateForSchedule.day;
    date.hour = dateForSchedule.hour;
    date.minute = dateForSchedule.minute;
} else if ([self.selectedFrequecy isEqualToString:YEARLY] || [self.selectedFrequecy isEqualToString:ONCE]) {
    date.month = dateForSchedule.month;
    date.day = dateForSchedule.day;
    date.hour = dateForSchedule.hour;
    date.minute = dateForSchedule.minute;
}

UNCalendarNotificationTrigger* trigger;

if ([_selectedFrequecy isEqualToString:ONCE]) {
    trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:date repeats:NO];
} else {
    trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:date repeats:YES];
}

// Create the request object.
UNNotificationRequest* request = [UNNotificationRequest requestWithIdentifier:self.callSchedule.id content:content trigger:trigger];

[center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error)
 {
     if (error != nil) {
         NSLog(@"%@", error.localizedDescription)
     }
 }];

这也不会等待3天,但会在第二天立即开火 .

两者的主要问题是它忽略了等待开始触发通知的时间 . 如果有人理解了这个问题,你会花些时间弄清楚什么是错的或者是否可能 . 我会非常感激的 .

1 回答

  • 0

    AppDelegate 取得许可 didfinishlauchingwithoption

    if( SYSTEM_VERSION_LESS_THAN( @"10.0" ) )
    {
    
        [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound |    UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
        [[UIApplication sharedApplication] registerForRemoteNotifications];
    }
    else
    {
        UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
        [center requestAuthorizationWithOptions:(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error)
         {
             if( !error )
             {
                 [[UIApplication sharedApplication] registerForRemoteNotifications];  // required to get the app to do anything at all about push notifications
                 NSLog( @"Push registration success." );
             }
             else
             {
                 NSLog( @"Push registration FAILED" );
                 NSLog( @"ERROR: %@ - %@", error.localizedFailureReason, error.localizedDescription );
                 NSLog( @"SUGGESTIONS: %@ - %@", error.localizedRecoveryOptions, error.localizedRecoverySuggestion );  
             }  
         }];  
    }
    

    ViewController - 在这里实施代表 -

    -(void)viewDidAppear:(BOOL)animated
    {
        [super viewDidAppear:animated];
        NSDate *theDate = [NSDate dateWithTimeIntervalSinceNow:60*1];
        NSDateComponents *dateForSchedule = [[[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian] components:NSCalendarUnitMinute|NSCalendarUnitHour|NSCalendarUnitDay|NSCalendarUnitWeekday|NSCalendarUnitMonth|NSCalendarUnitYear fromDate:theDate];
        UNMutableNotificationContent* content = [[UNMutableNotificationContent alloc] init];
        content.body = @"Your notification is here.";
        content.sound = [UNNotificationSound defaultSound];
        content.categoryIdentifier = CALL_NOTIFICATION_CATEGORY;
    
        NSDateComponents* date = [[NSDateComponents alloc] init];
        date.month = dateForSchedule.month;
        date.day = dateForSchedule.day;
        date.hour = dateForSchedule.hour;
        date.minute = dateForSchedule.minute;
    
        UNCalendarNotificationTrigger* trigger;
        trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:date repeats:NO];
    
        UNUserNotificationCenter *centre = [UNUserNotificationCenter currentNotificationCenter];
        centre.delegate = self;
        // Create the request object.
        UNNotificationRequest* request = [UNNotificationRequest requestWithIdentifier:self.callSchedule.id content:content trigger:trigger];
    
        [centre addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error)
         {
             if (error != nil) {
                 NSLog(@"%@", error.localizedDescription);
             }
         }];
    }
    
    -(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler{
    
        //Called when a notification is delivered to a foreground app.
    
        NSLog(@"Userinfo %@",notification.request.content.userInfo);
    
        completionHandler(UNNotificationPresentationOptionAlert);
    }
    
    -(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)(void))completionHandler{
    
        //Called to let your app know which action was selected by the user for a given notification.
    
        NSLog(@"Userinfo %@",response.notification.request.content.userInfo);
    
    }
    

    我能够在正确的时间收到通知 . 我已经从代码片段中获取了所有代码,只需要进行非常小的更改即可运行代码 . 你可以看到你是否发现了不同的东西 .

    希望这个建议可以帮到你!

相关问题