UILocalNotification repeatInterval 20 天 [英] UILocalNotification repeatInterval on 20 days

查看:35
本文介绍了UILocalNotification repeatInterval 20 天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用自定义间隔重复(例如每 20 天)创建本地通知.我知道我们有 NSDayCalendarUnit、kCFCalendarUnitMonth ......但我希望将重复间隔设置为 20 天.我不想每天都创建通知.

I'd like to create local notification with custom interval repeat (every 20 days for example). I know we have NSDayCalendarUnit, kCFCalendarUnitMonth ... but I hope to set repeat interval at 20 days. I don't want to create a notification for every day.

我真正需要的是连续 21 天重复通知,然后在 7 天后不要启动它,然后是新的 21 天有通知和 7 天没有......等等.即使应用程序处于非活动状态,我也应该安排所有这些天.

My real need is to repeat a notification for consecutive 21 days, then don't launch it for 7 days later, then a new 21 days with notification and 7 days without ... etc. I should schedule all these days even if application is inactive.

为了做到这一点,我决定创建 21 条通知,因为它的触发日期为 repeatInterval = 28days(这是问题所在)

To do this I decide to create a 21 notifications since a fire date with repeatInterval = 28days (here is the problem)

推荐答案

试试这个,你可以通过选择 setDay, setMonth, .... 来改变间隔:

Try this, you can change the intervall by selecting setDay, setMonth, .... :

NSCalendar *calendar = [NSCalendar currentCalendar];

NSDateComponents *components = [[NSDateComponents alloc] init];
[components setDay:3];

NSDate *date3Days = [calendar dateByAddingComponents:components 
                                               toDate:[NSDate date] 
                                              options:0];

UIApplication* app = [UIApplication sharedApplication];
NSArray*    oldNotifications = [app scheduledLocalNotifications];
if ( oldNotifications ) {
    [app cancelAllLocalNotifications];
    app.applicationIconBadgeNumber = 0;
}

UILocalNotification* notifyAlarm = [[UILocalNotification alloc] init];
if (notifyAlarm) {
    notifyAlarm.fireDate = date3Days;
    notifyAlarm.timeZone = [NSTimeZone defaultTimeZone];
    notifyAlarm.alertBody = NSLocalizedString( @"Push message", @"");
    notifyAlarm.soundName = @"sound.wav";
    [app scheduleLocalNotification:notifyAlarm];
}

<小时>

如果您想设置 UILocalNotifications 出现的特定时间,您可以创建上述解决方案的方法并循环遍历一个数组,该数组指示您希望显示通知的日期:


If you want to set a specific time after which the UILocalNotifications should appear you can create a method of the above solution and loop over an array which indicates the days you like to show a notification:

NSArray *arrayNumbers = @[@5, @7, @14, @21, @30, @60, @90, @120];
NSDictionary *dictNotifications = 
[[NSUserDefaults standardUserDefaults]    objectForKey:kUserDefAppStarts];

for ( NSNumber *bla in arrayNumbers ){

    NSString *strKey = [NSString stringWithFormat:@"%@%@", kUserDefAppStarts, bla];
    NSDictionary *dict = dictNotifications[strKey];
    NSString *strMessageQuestion = dict[kKeyMessage];

    [self createNotificationWithNumberOfDays:[bla integerValue]
                                  andMessage:strMessageQuestion
                                    userInfo:dict];
}

这是你必须调用的方法

+ (void)createNotificationWithNumberOfDays:(int)days 
                                andMessage:(NSString *)message 
                                  userInfo:(NSDictionary *)dict{

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [[NSDateComponents alloc] init];
[components setDay:days];

NSDate *dateAlert = [gregorian dateByAddingComponents:components toDate:[NSDate date] options:0];

UIApplication *app = [UIApplication sharedApplication];
UILocalNotification *notifyAlarm = [[UILocalNotification alloc] init];

if( notifyAlarm ){
    [notifyAlarm setFireDate:dateAlert];
    [notifyAlarm setTimeZone:[NSTimeZone defaultTimeZone]];
    [notifyAlarm setSoundName:@"soundname"];
    [notifyAlarm setAlertBody:message];
    [notifyAlarm setUserInfo:dict];
    [app scheduleLocalNotification:notifyAlarm];
}

}

这篇关于UILocalNotification repeatInterval 20 天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆