gpt4 book ai didi

ios - UILocalNotification repeatInterval 20 天

转载 作者:行者123 更新时间:2023-11-29 10:35:28 26 4
gpt4 key购买 nike

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

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

为此,我决定创建一个 21 条通知,自触发日期起,repeatInterval = 28days(这里是问题所在)

最佳答案

试试这个,您可以通过选择 setDaysetMonth、.... 来更改间隔:

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 应该出现,你可以创建一个上述解决方案的方法并循环一个数组,它指示你想要显示通知的日期:

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

关于ios - UILocalNotification repeatInterval 20 天,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27424003/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com