Reputation: 742
I Want Random Local notification once every 24 hours.
I know , i Can have Daily Local Notification Using this :
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];
But ,With This I can Have Same Time Every Day ,But How can I have a Random Time EveryDay. Please Help !
Even, Is this Possible ?
Upvotes: 0
Views: 701
Reputation: 13045
Clean answer
This schedules a notification for the next 64 days. Good place to set it is in didFinishLaunchingWithOptions:
because it cancelAllLocalNotifications
and set 64 notifications for the future. So every time the user opens the app it clears and reschedule for the next 64 days from now.
[[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];
}
Output:
...
[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
...
Upvotes: 0
Reputation: 1394
For anybody looking to do this in Swift you can do something like this:
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")
}
The above will schedule random notifications for the next 48 days, making sure those notifications only fire at a reasonable time of the day.
You can call this from applicationDidBecomeActive()
func applicationDidBecomeActive(application: UIApplication) {
UIApplication.sharedApplication().applicationIconBadgeNumber = 0
scheduleNotfications()
}
Upvotes: 0
Reputation: 349
According to UILocalNotification class reference you could schedule up to 64 local notifications to fire at exact time. It is enough to cover a couple months of random timed notifications since every app launch. Here is a sample:
- (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];
}
}
Upvotes: 1