美文网首页
本地推送~多个日期

本地推送~多个日期

作者: henu_Larva | 来源:发表于2018-08-20 17:19 被阅读12次
- (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];
}

相关文章

网友评论

      本文标题:本地推送~多个日期

      本文链接:https://www.haomeiwen.com/subject/gfiliftx.html