如何在Objective C中的一天或24小时后显示本地通知?

时间:2017-03-07 11:02:30

标签: ios objective-c

我是iOS新手,我在显示本地通知时遇到问题。

以下是我的代码。

AppDelegate.m

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

}

在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];

通过使用此代码通知在一分钟后调用。但我需要在24小时后给它打电话。

我该怎么做?

提前致谢!

5 个答案:

答案 0 :(得分:3)

请更新以下内容 -

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

答案 1 :(得分:2)

试试这个

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

答案 2 :(得分:1)

试试这个:

- (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];
}

在此之前应该驳回所有通知......

答案 3 :(得分:0)

here

上有一个重复的问题

have a look at NSDateComponents

答案 4 :(得分:0)

之前已经提出过这个问题,请仔细阅读此链接并解决您的问题。

How to implement LocalNotification using objective C?

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

此外,我必须说,如果您使用的是iOS 10,那么您必须注册本地通知,这意味着您允许在预期时间内通知您的通知。