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

//定时推送

+ (void)createLocalizedUserNotification{

 

    // 设置触发条件 UNNotificationTrigger

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

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

 

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

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

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

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

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

        }

    }];

 

}

转载于:https://my.oschina.net/u/242157/blog/3030686

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值