Muju
Muju

Reputation: 989

How to show local notification after one day or 24 hours in Objective C?

I am new in iOS and I am facing problem regarding to show local notification.

Below is my code.

AppDelegate.m

-(void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings{

}

In viewDidLoad()

 defaultsnotification = [NSUserDefaults standardUserDefaults];

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

  UILocalNotification* localNotification = [[UILocalNotification alloc] init];
  localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:0]; //Enter the time here in seconds.
  localNotification.alertBody = @"app Update Available";
  localNotification.timeZone = [NSTimeZone defaultTimeZone];
  localNotification.repeatInterval = NSCalendarUnitDay; //Repeating instructions here.
  localNotification.soundName = UILocalNotificationDefaultSoundName;
  [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

By using this code notification is calling After a minute. But I need to call it after 24 hours.

How can I do That?

Thanks in Advance!

Upvotes: 1

Views: 907

Answers (5)

Sathish Kumar VG
Sathish Kumar VG

Reputation: 2172

Try this :

- (void)applicationDidEnterBackground:(UIApplication *)application
{
UILocalNotification *notification = [[UILocalNotification alloc] init];
  notification.fireDate = [[NSDate date] dateByAddingTimeInterval:60*60*24];
 notification.alertBody = @"24 hours passed since last visit :(";
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}

Before that should dismiss all Notification....

Upvotes: 1

Abhishek Mitra
Abhishek Mitra

Reputation: 3395

This question has been asked before, Please go through this links and solve your problem.

How to implement LocalNotification using objective C?

How to schedule daily local push notification in iOS (ObjC)?

In addition, i have to say, if you are using iOS 10 then you have to register your local notification, it means you are permitting your notification to fire on a desired time.

Upvotes: 0

Pooja Gupta
Pooja Gupta

Reputation: 793

Please update the following-

localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:(24*60*60)];

Upvotes: 3

Abhishek
Abhishek

Reputation: 494

A duplicate question is there on here

have a look at NSDateComponents

Upvotes: 0

RajeshKumar R
RajeshKumar R

Reputation: 15788

Try this

localNotification.fireDate = [[NSDate date] dateByAddingTimeInterval:(60*60*24)];

Upvotes: 2

Related Questions