2015-12-03 105 views
1

代碼註冊得到通知是:如何重複本地通知後的iOS 814天和9

UIUserNotificationType types = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert; 
     UIUserNotificationSettings *mySettings = [UIUserNotificationSettings settingsForTypes:types categories:nil]; 
     [[UIApplication sharedApplication] registerUserNotificationSettings:mySettings]; 

和代碼調度通知:

UILocalNotification *notification = [[UILocalNotification alloc] init]; 
[self setNotificationTypesAllowed]; 
if (notification) 
{ 
    if (allowNotif) 
    { 
     notification.timeZone = [NSTimeZone defaultTimeZone]; 
     if ([statusString isEqualToString:@"daily"]) { 
      notification.fireDate = _timePick.date; 

       notification.repeatInterval = NSCalendarUnitDay; 
     }else if ([statusString isEqualToString:@"weekly"]) { 
      notification.fireDate = _timePick.date; 

       notification.repeatInterval = NSCalendarUnitWeekOfYear; 

     }else if ([statusString isEqualToString:@"fortnightly"]) { 
      notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:60*60*24*14];; 
      notification.repeatInterval = NSCalendarUnitMinute; 

      //notification.repeatInterval = NSCalendarUnitYear; 
     }else{ 
       notification.repeatInterval = 0; 
     } 
    } 
    if (allowsAlert) 
    { 
     notification.alertBody = [NSString stringWithFormat:@"Do you really want to send message to %@",name]; 
    } 
    if (allowsBadge) 
    { 

     notification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1; 
    } 
    if (allowsSound) 
    { 
     notification.soundName = UILocalNotificationDefaultSoundName; 
    } 
    notification.alertAction = @"Yes"; 
    notification.timeZone = [NSTimeZone defaultTimeZone]; 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showSMS) name:@"showSMS" object:nil]; 

    // this will schedule the notification to fire at the fire date 
    [[UIApplication sharedApplication] scheduleLocalNotification:notification]; 

我能每週重複本地通知&每週但不重複每兩週,請幫助

回答

5

是爲@Nikos建議,試試下面的代碼:

  1. 取消UILocalNotification。

    [[UIApplication sharedApplication] cancelAllLocalNotifications]; 
    
  2. 安排本地通知

    UILocalNotification *notification = [[UILocalNotification alloc]init]; 
    notification.userInfo = @{@"notification_identifier":@"After14Days"}; 
    notification.fireDate = [[NSDate date] dateByAddingTimeInterval:(60*60*24*14)]; 
    notification.alertBody = @"Text to display"; 
    notification.repeatInterval = NSCalendarUnitDay; 
    [[UIApplication sharedApplication] scheduleLocalNotification:notification]; 
    

如果您有任何疑問,然後讓我知道。

+0

嗨Ekta 感謝它的工作 –

+0

上述代碼將不會每14天后重複通知,如果用戶30天未打開應用程序。 –

+0

@RamaniAshish甚至不是第一次? –

0

要設置通知每14天觸發一次,您必須創建26個單獨的UILocalNoti repeatInterval設置爲NSYearCalendarUnit(重複間隔必須設置爲日曆單位,iOS中沒有14天的CalendarUnit)。

另一種處理方法是取消didReceiveLocalNotification方法中的UILocalNotification,並在14天后安排一個新方法。儘管此方法假定用戶與通知交互,否則下一個將永遠不會被安排。