【问题标题】:Trigger local notifications automatically daily on dynamic time given in arrays Objective c ios每天在数组中给定的动态时间自动触发本地通知Objective c ios
【发布时间】:2018-09-26 04:31:51
【问题描述】:

我有一个小时数组hoursArray 和分钟数组minutesArray,我现在从系统中获取current date 我有包含当月元素的数组,这意味着如果有 30 天在四月份,我在数组中插入的 hoursArray/minutesArray 中将有 30 小时/分钟,并且我将当前日期作为数组的索引。

我所做的是通知在当天触发,但直到我每天使用应用程序才会在第二天触发,因为当我每天使用应用程序之前,当我切换到后台模式和通知响铃时会调用触发时间方法.

现在我希望在日期更改时自动触发通知,即使我有几天不使用该应用程序,这些东西都应该在appDelegatedidEnterBackground... 方法中

我关注this answer,但它每天用于同一时间但我希望每天与基于当前日期的数组索引的数组不同的时间(这意味着当天,例如今天是 4 月 19 日,小时和分钟的索引数组应该是 19)。

这是我的数组的样子

小时数组 = [9, 10, 11, 13, 14, 11, 17, 2, 15, 5.... 等等]

分钟数组 = [23, 00, 04, 58, 59, 12, 01, 33,.... 等]

appdelegate.m中的方法调用

- (void)applicationDidEnterBackground:(UIApplication *)application
{     
 [self.viewController triggerAutomaticallyDaily];
 [self applicationSignificantTimeChange:application];
 [self refreshAlarm];
}

viewcontroller.m内部的方法

-(void)triggerAutomaticallyDaily {
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar] ;
NSDate *now = [NSDate date];

NSDateComponents *components = [calendar components:(NSCalendarUnitYear | NSCalendarUnitMonth |  NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute) fromDate:now]; 
[components setHour:hoursArray[currentDay]]; //currentDay the todays date is 16 I am getting current date from system.
[components setMinute:minutesArray[currentDay]];

UILocalNotification *notification = [[UILocalNotification alloc]init];
notification.fireDate = [calendar dateFromComponents:components];
notification.repeatInterval = NSDayCalendarUnit;
[notification setAlertBody:@"This is your task time"];
// notification.soundName = UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication] scheduleLocalNotification:notification]; 
}

每个午夜后显着时间变化,Appdelegate中的方法

-(void)applicationSignificantTimeChange:(UIApplication *)application {
[self.viewController triggerAutomaticallyDaily];
}

Appdelegate中的刷新报警方法

- (void)refreshLabel
{
//refresh the alarm on the main thread
dispatch_async(dispatch_get_main_queue(),^{      
[self.viewController triggerAutomaticallyDaily];
});
// check every 10000s
[self performSelector:@selector(refreshLabel) withObject:nil afterDelay:10000]; 
}

看看didEnterBackground...,当我退出我的应用程序时,通知方法只会被调用一次。不是吗??以及当我一周没有打开应用程序但我想收到通知时,我如何收到每日通知,将如何调用方法?是在后台模式下每次都调用方法吗?

有什么办法可以让我安排通知,当第一个通知完成时,第二个应该被触发,如果第二个完成,那么第三个应该被触发,等等在后台模式下,即使我没有打开应用程序一周??应根据数组中给出的当前日期和时间触发通知。

更新 我什至设置了刷新功能,它每 10000 秒刷新一次触发器,但它不起作用。

我还添加了 significantTimechanges ,如果有午夜变化,但它不起作用到这里是我如何定义的。

【问题讨论】:

  • 你有多少天的数据?下次您何时调用您的服务来刷新数据,因为响应中没有指定日期。
  • @TheTiger 我有几个月的数据,日期有效期为一个月
  • 您的意思是您有 3 月的 31 天数据和 2 月的 28 天数据?
  • 你有多少数据?还有无限的几个月 --> 几年。
  • 首先如果用户不打开应用,那么你什么都做不了,所以忘记这个成就。这种情况应该在服务器端处理,所以最好使用推送通知而不是本地通知。

标签: ios objective-c unnotificationtrigger


【解决方案1】:

重要提示:如果应用程序长时间处于终止状态或后台,开发人员将无法执行任何操作。所以这是做不到的。

您的要求有很多问题:

1.1:您在一项服务中有多少个days datafor a monthfor a year 或?

1.2:为什么不push

1.3:您为什么要每天安排local notification,而您可以一次安排多个?

1.4:app 未打开(被用户杀死)时无法对其进行更改,因此在这种情况下您无法执行任何操作。

现在来看看我们能做什么:

2.1:考虑到您正在获取月度数据。所以你应该通过增加day component并设置相应的时间(from your arrays by day index)来一次在for loop中调度所有local notifications。如果用户每个月打开一次应用程序(90% 的机会他会打开),那么你就可以向服务器请求下个月的数据并做同样的事情。

2.2:为什么不在服务器端处理这个问题并使用push notifications 而不是local。服务器人员可以为每分钟配置一个 cron 作业,它将为用户获取所有小时和分钟,并将发送 push

我的建议不要使用local,但您可以使用2.1(一次安排所有月份的通知)。

【讨论】:

  • 1.1,目前我有一个飞蛾数据,比如 4 月 30 天的 30 个数据,或者我可以存储一年数据 365 个数据并按当前日期计算当前日期的当前数
  • 1.2 我想设置服务器端的东西,所以我不使用推送
  • 1.3 因为我无法存储整年左右的通知,而且我的数据当前位于静态数组中,然后我使用 json 文件从 json 获取数据,如果用户从设置或增加通知的时间,同时触发会很奇怪
  • 1.4 我可以使用完成处理程序吗?或者当通知触发时,应该调用其他通知
  • @iOSDeveloper 请在回复所有内容时牢记如果应用程序被杀死,您将无能为力。
【解决方案2】:

只需使用下面的语句而不是NSCalendar

UILocalNotification *notification = [[UILocalNotification alloc]init];
notification.fireDate = [[NSDate date] dateByAddingTimeInterval:(hours*3600)+(Mins*60)];

//时间间隔单位应该是秒

notification.repeatInterval = NSDayCalendarUnit;
[notification setAlertBody:@"This is your task time"];
[[UIApplication sharedApplication] scheduleLocalNotification:notification]; 

【讨论】:

  • 亲爱的我试过了,明天看看会不会自动发生,希望会好。
  • 你可以用更短的时间间隔测试它,这样你就不用等一天了。
  • @Koen 但我想每天定时重复?这可能吗?
  • 只需将设备时间更改为您的目标时间。然后通知会自动出现。
  • @iMHitesh 查看更新的问题,当我退出我的应用程序时,通知方法将只调用一次。不是吗??以及当我一周没有打开应用但我想收到通知时,我如何收到每日通知?
【解决方案3】:

正如其他人所说,更好的技术是一次安排所有通知。这就是我所做的。但是,系统限制为 64 个待处理的本地通知。在您的情况下,计划的通知应在 64 天内每天触发。我有一个更困难的问题,因为我需要每 15 分钟安排一次通知。因此,如果我的用户在 16 小时内没有回复,他们将不会收到进一步的通知 (64/4 = 16)。另外,请注意 UILocalNotification 已被弃用,因此您应该使用 UNNotificationRequest。请注意,UNCalendarNotificationTrigger 中的标识符对于每个请求必须是唯一的。

    content.categoryIdentifier = @"com.nelsoncapes.localNotification";
    NSDate *today = [NSDate date];
    NSDate *fireDate = [today dateByAddingTimeInterval:interval];
    // first extract the various components of the date
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSInteger year = [calendar component:NSCalendarUnitYear fromDate:fireDate];
    NSInteger month = [calendar component:NSCalendarUnitMonth fromDate:fireDate];
    NSInteger day = [calendar component:NSCalendarUnitDay fromDate:fireDate];
    NSInteger hour = [calendar component:NSCalendarUnitHour fromDate:fireDate];
    NSInteger minute = [calendar component:NSCalendarUnitMinute fromDate:fireDate];
    NSDateComponents *components = [[NSDateComponents alloc]init];
    components.year = year;
    components.month = month;
    components.day = day;
    components.hour = hour;
    components.minute = minute;
…
// construct a calendarnotification trigger and add it to the system
    UNCalendarNotificationTrigger *trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:components repeats:NO];
    UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier: identifier content:content trigger:trigger];
    [center addNotificationRequest:request withCompletionHandler:^(NSError *error){
        if(error){
        NSLog(@"error on trigger notification %@", error);
        }
    }];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-10
    • 1970-01-01
    相关资源
    最近更新 更多