本地定时推送代码实现 IOS10以上

//定时推送.net

+ (void)createLocalizedUserNotification{orm

 

    // 设置触发条件 UNNotificationTrigger对象

    //timeInterval:单位为秒(s)repeats:是否循环提醒get

    UNTimeIntervalNotificationTrigger *timeTrigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:5 repeats:YES];string

 

    // 建立通知内容 UNMutableNotificationContent, 注意不是 UNNotificationContent ,此对象为不可变对象。it

    UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];io

    content.title = @"Dely 时间提醒 - title";table

    content.subtitle = [NSString stringWithFormat:@"Dely 装逼大会竞选时间提醒 - subtitle"];class

    content.body = @"Dely 装逼大会总决赛时间到,欢迎你参加总决赛!但愿你一统X界 - body";循环

    content.badge = @666;

    content.sound = [UNNotificationSound defaultSound];

    content.userInfo = @{@"key1":@"value1",@"key2":@"value2"};

 

    // 建立通知标示

    NSString *requestIdentifier = @"Dely.X.time";

 

    // 建立通知请求 UNNotificationRequest 将触发条件和通知内容添加到请求中

    UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:requestIdentifier content:content trigger:timeTrigger];

 

    UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];

    // 将通知请求 add 到 UNUserNotificationCenter

    [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {

        if (!error) {

            NSLog(@"推送已添加成功 %@", requestIdentifier);

            //你本身的需求例以下面:

            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"本地通知" message:@"成功添加推送" preferredStyle:UIAlertControllerStyleAlert];

            UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];

            [alert addAction:cancelAction];

            [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alert animated:YES completion:nil];

        }

    }];

 

}

相关文章
相关标签/搜索