- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.isActive = YES;
// 设置通知的类型可以为弹窗提示,声音提示,应用图标数字提示
UIUserNotificationSettings *setting = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert categories:nil];
// 授权通知
[[UIApplication sharedApplication] registerUserNotificationSettings:setting];
[[UIApplication sharedApplication] cancelAllLocalNotifications];
return YES;
}
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
if (self.isActive) {
UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"温馨提示" message:notification.alertBody preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
[alert addAction:okAction];
[self.window.rootViewController presentViewController:alert animated:YES completion:nil];
}
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = @"yyyy-MM-dd";
NSDate *date = [dateFormatter dateFromString:@"2018-08-27"];
[self localNotification:date];
}
- (void)localNotification:(NSDate *)date {
NSTimeInterval targetTimeInterval = [date timeIntervalSince1970];
NSTimeInterval beyond = [[NSDate date] timeIntervalSince1970];
// 提前 7天
NSTimeInterval sevenDayTimeInterval = targetTimeInterval - (60 * 60 * 24 * 7) + (60 * 60 * 9);
// 提前 3天
NSTimeInterval threeDayTimeInterval = targetTimeInterval - (60 * 60 * 24 * 3) + (60 * 60 * 9);
// 提前 1天
NSTimeInterval oneDayTimeInterval = targetTimeInterval - (60 * 60 * 24 * 1) + (60 * 60 * 9);
// 计算是否超过某天
if ((beyond - sevenDayTimeInterval) <= 0) {
[self addLocalNotification:sevenDayTimeInterval content:@"qi"];
}
if ((beyond - threeDayTimeInterval) <= 0) {
[self addLocalNotification:threeDayTimeInterval content:@"san"];
}
if ((beyond - oneDayTimeInterval) <= 0) {
[self addLocalNotification:oneDayTimeInterval content:@"yi"];
}
}
- (void)addLocalNotification:(NSTimeInterval)timeInterval content:(NSString *)content {
NSDate *fireDate = [NSDate dateWithTimeIntervalSince1970:timeInterval];
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.alertBody = content;
localNotification.fireDate = fireDate;
localNotification.soundName = UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。