2014-03-29 29 views
0

我希望每24小時隨機本地通知一次。
我知道,我能有每日地方通知使用此:如何使用隨機文本在隨機時間在一天內安排一次本地通知

UILocalNotification *localNotification = [[UILocalNotification alloc] init]; 
localNotification.fireDate = fireDate; 
localNotification.timeZone = [NSTimeZone defaultTimeZone]; 
localNotification.repeatInterval = NSDayCalendarUnit; 
localNotification.alertBody = alertText; 
localNotification.alertAction = alertAction;  

// Schedule it with the app 

[[UIApplication sharedApplication] scheduleLocalNotification:localNotification]; 
[localNotification release]; 

但是,有了這個,我可以有每天同一時間,但我怎麼能有一個隨機的時間每一天。 請幫忙!

即使這是可能的嗎?

+0

localNotification.repeatInterval = NSDayCalendarUnit;這條線每天都會觸發本地通知在同一時間,但我想在隨機時間 –

回答

1

根據UILocalNotification class reference您可以安排最多64個本地通知在準確時間觸發。自從每次發佈應用程序以來,這已經足以涵蓋幾個月的隨機定時通知。下面是一個示例:

- (void)scheduleLocalNotifications 
{ 
    [[UIApplication sharedApplication] cancelAllLocalNotifications]; 

    static NSInteger dayInSeconds = 60*60*24; 
    NSInteger now = (NSInteger)[NSDate timeIntervalSinceReferenceDate]; 
    NSInteger tomorrowStart = now - now % dayInSeconds + dayInSeconds; 
    for (int q=0; q<64; ++q) 
    { 
     NSInteger notificationTime = tomorrowStart + q*dayInSeconds + rand()%dayInSeconds; 
     NSDate * notificationDate = [NSDate dateWithTimeIntervalSinceReferenceDate:notificationTime]; 
     NSLog(@"date %@", notificationDate); 

     UILocalNotification * notification = [UILocalNotification new]; 
     notification.fireDate = notificationDate; 
     notification.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0]; 
     notification.soundName = UILocalNotificationDefaultSoundName; 
     notification.alertBody = @"Hello!"; 
     [[UIApplication sharedApplication] scheduleLocalNotification:notification]; 
    } 
} 
+0

感謝所以這會在每次啓動應用程序時觸發! ñ它會得到附表每次我啓動應用程序? &如果是這樣,你的意思是我必須在64天后重新安排它,如果應用程序沒有在這個時間間隔推出... –

+0

@shaqirsaiyed我的意思是你應該在每次啓動後重新安排所有64個通知。這將使通知在最後一次發佈後的64天內發起。在安排新的通知之前,請不要忘記取消之前的通知。 –

+0

雅我知道了。謝謝。我正在做同樣的事情。但我無法設置在每一天你的這行NSInteger notificationTime = tomorrowStart + q * dayInSeconds + rand()%dayInSeconds;有時候,我在同一天收到了兩條通知......你能指導一下嗎?我希望每天只有一個通知.. –

0

任何人要做到這一點斯威夫特,你可以做這樣的事情:

func scheduleNotfications() { 
     print("Scheduling reminder notifications") 

     UIApplication.sharedApplication().cancelAllLocalNotifications() 

     let windowInSeconds: UInt32 = 60*60*5 
     let oneDayInSeconds: Double = 60*60*24 
     let windowAroundHour = 14 

     let calendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian) 
     calendar?.timeZone = NSTimeZone.localTimeZone() 

     if let todayAtWindowHour = calendar?.dateBySettingHour(windowAroundHour, minute: 0, second: 0, ofDate: NSDate(), options: .MatchFirst)?.timeIntervalSinceReferenceDate { 
      for index in 1...48 { 
       var notificationDate = (todayAtWindowHour + (Double(index) * oneDayInSeconds)) 

       // either remove or add anything up to the window 
       if arc4random_uniform(2) == 0 { 
        notificationDate = notificationDate + Double(arc4random_uniform(windowInSeconds)) 
       } else { 
        notificationDate = notificationDate - Double(arc4random_uniform(windowInSeconds)) 
       } 

       let fireDate = NSDate(timeIntervalSinceReferenceDate: notificationDate) 

       let notification = UILocalNotification() 
       notification.alertBody = NSLocalizedString("You've received a new notification.", comment: "Notification") 
       notification.fireDate = fireDate 
       notification.timeZone = NSTimeZone.defaultTimeZone() 
       notification.applicationIconBadgeNumber = 1 
       UIApplication.sharedApplication().scheduleLocalNotification(notification) 

       print("Set for: \(fireDate)") 
      } 
     } 

     print("Finished scheduling reminder notifications") 
    } 

以上將安排隨機通知在接下來的48天,確保這些通知只能在一天中合理的時間開火。

您可以從applicationDidBecomeActive稱之爲()

func applicationDidBecomeActive(application: UIApplication) { 
    UIApplication.sharedApplication().applicationIconBadgeNumber = 0 
    scheduleNotfications() 
} 
0

清潔答案

這安排的通知的下一個64天。設置它的好地方是didFinishLaunchingWithOptions:,因爲它爲cancelAllLocalNotifications設置了64個未來通知。因此,每次用戶打開應用程序時,都會在接下來的64天內清除並重新安排時間。

[[UIApplication sharedApplication] cancelAllLocalNotifications]; 

NSDate *givenDate = [NSDate date]; // set your start date here 
NSCalendar *calendar = [NSCalendar currentCalendar]; 
[calendar setTimeZone:[NSTimeZone localTimeZone]]; 

NSDateComponents *components = [calendar components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute) fromDate:givenDate]; 

NSDateComponents *dateComps = [NSDateComponents new]; 
[dateComps setYear:components.year]; 
[dateComps setMonth:components.month]; 
[dateComps setDay:components.day]; 
[dateComps setHour:components.hour]; 
[dateComps setMinute:components.minute]; 
[dateComps setSecond:0]; 
NSDate *notificationDate = [calendar dateFromComponents:dateComps]; 

for (int x = 0; x < 64; x++) { 
    UILocalNotification *notification = [UILocalNotification new]; 
    [notification setFireDate:notificationDate]; 
    [notification setTimeZone:[NSTimeZone localTimeZone]]; 
    [notification setSoundName:UILocalNotificationDefaultSoundName]; 
    [notification setAlertBody:@"My notification body!"]; 
    [[UIApplication sharedApplication] scheduleLocalNotification:notification]; 

    notificationDate = [NSDate dateWithTimeInterval:86400 sinceDate:notificationDate]; 
} 

輸出:

... 
[26] Date : 2016-06-29 17:11:00 +0000 
[27] Date : 2016-06-30 17:11:00 +0000 
[28] Date : 2016-07-01 17:11:00 +0000 
[29] Date : 2016-07-02 17:11:00 +0000 
... 
相關問題