-1

Good Morning, How to notify localNotification for every two weeks (14days) a notification.

- (void)applicationDidEnterBackground:(UIApplication *)application {

    timer = [NSTimer scheduledTimerWithTimeInterval:60*60*24*14 target:self selector:@selector(getNotifiedForTwoWeeks:) userInfo:nil repeats:YES];


}

-(void)getNotifiedForTwoWeeks:(id)userinfo{

        // Schedule the notification
    UILocalNotification* localNotification = [[UILocalNotification alloc] init];
    localNotification.fireDate =  [NSDate date];
    localNotification.alertBody = @"Notification Message";
    localNotification.alertAction = @"Show me the item";
    localNotification.timeZone = [NSTimeZone timeZoneWithName:@"GMT"];
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}

Please let me know is this implementation is correct or not? Is there any alternative way i can do the best for notifying a LocalNotification message for every two weeks.

Your valuable inputs are appreciated!

KkMIW
  • 1,092
  • 1
  • 17
  • 27
  • I'm not sure what this `NSTimer` is for, as that's only going to work if the app is active for 2 weeks. You should update the `fireDate` to be some date in the future (and you should perform `NSCalendar` calandrical calculations rather than 60*60*24*12). See http://stackoverflow.com/a/27424355/1271826. – Rob Aug 15 '16 at 18:22
  • If it were every week or every month, rather than every two weeks, you could use `repeatInterval`, but for every two weeks, I think you have to schedule them yourself, as shown in that answer. – Rob Aug 15 '16 at 18:24
  • @NSTimer is used to schedule for TimeInterval to trigger the specific method for given notification declared method. – kiran Aug 18 '16 at 08:40

1 Answers1

0

When your app will enter to background the system will suspend it after some time and your NSTimer will not schedule. So you can schedule for every 14 days notification through declare a fireDate and repeatInterval properties in UILocalNotification

UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate =  [NSDate dateByAddingTimeInterval:60*60*24*14];
localNotification.alertBody = @"Notification Message";
localNotification.alertAction = @"Show me the item";
localNotification.timeZone = [NSTimeZone timeZoneWithName:@"GMT"];
localNotification.repeatInterval = NSDayCalendarUnit;

[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
iSashok
  • 2,326
  • 13
  • 21
  • Needless to say, `beginBackgroundTaskWithName` is useless when it comes to a timer scheduled for 60*60*24*14. The whole timer thing is dead end for this use case, anyway. It's good for a few minutes, not a few weeks. – Rob Aug 15 '16 at 21:57
  • Yeah you are right – iSashok Aug 16 '16 at 03:58