如何使用随机文本在随机时间一天安排本地通知

时间:2014-03-29 07:28:09

标签: cocos2d-iphone

我希望每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];

但是,有了这个我每天都可以有相同的时间,但我怎么能每天都有随机时间。  请帮助!

甚至,这可能吗?

3 个答案:

答案 0 :(得分: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];
    }
}

答案 1 :(得分:0)

对于任何希望在Swift中执行此操作的人,您可以执行以下操作:

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()
}

答案 2 :(得分: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
...