簡體   English   中英

UILocalNotification repeatInterval在20天

[英]UILocalNotification repeatInterval on 20 days

我想創建具有自定義間隔重復的本地通知(例如,每20天)。 我知道我們有NSDayCalendarUnit,kCFCalendarUnitMonth ...,但我希望將重復間隔設置為20天。 我不想每天都創建一個通知。

我真正的需要是連續21天重復一次通知,然后在7天后不啟動它,然后再進行21天有通知的工作,而在7天沒有通知的情況下等。我應該安排所有這些天,即使申請是無效。

為此,我決定自發射日期起使用repeatInterval = 28days創建21條通知(這是問題所在)

嘗試此操作,可以通過選擇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];
}

}

如果您希望從當前時間開始將UILocalNotification安排在未來的20天,請執行以下操作:

NSCalendar *currentCalendar = [NSCalendar currentCalendar];
NSDateComponents *dateComp = [[NSDateComponents alloc] init];
int desiredAmountOfMonths = 4;
for (int month = 0; month < desiredAmountOfMonths; month++)
{
    dateComp.month = month;
    dateComp.day = 20;
    NSDate *fireDate = [currentCalendar dateByAddingComponents:dateComp toDate:[NSDate date] options:NSCalendarMatchNextTimePreservingSmallerUnits];

    UILocalNotification *notification = [[UILocalNotification alloc] init];
    notification.fireDate = fireDate;
    notification.timeZone = [NSTimeZone defaultTimeZone];
}

您將需要修改UILocalNotification的消息,聲音等內容,然后在自定義完成后觸發通知。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM