IOS本地通知

本文介绍了在iOS10及以上版本中如何处理本地通知的变化,包括如何判断设备版本并采用相应通知写法,以及展示发送本地通知的代码和代理方法的使用,实现了特定的通知效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

由于IOS10以后更新了通知的类。所以我们必须要学会判断当前设备的版本,ios10以前的设备仍然使用以前的写法。ios10以后使用新写法。如果还不会判断版本的。请参考上一篇微博:
http://blog.youkuaiyun.com/lee727n/article/details/72757831
发送本地通知代码及注释如下:

- (void)viewDidLoad {
    [super viewDidLoad];

//    ios10以前的写法:
//    UILocalNotification *noti = [[UILocalNotification alloc]init];
//    noti.userInfo = @{@"name":@"hehe"};
//    
//    noti.repeatInterval = NSCalendarUnitMinute;
//    
//    noti.alertBody = @"lalala body";
//    
//    noti.applicationIconBadgeNumber = 5;
//    //开始时间
//    noti.fireDate = [[NSDate new] dateByAddingTimeInterval:5];
//    
//    [[UIApplication sharedApplication] scheduleLocalNotification:noti];





    //iOS 10以后的写法
    UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];

     [center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert) completionHandler:^(BOOL granted, NSError * _Nullable error) {
        if (!error) {
            NSLog(@"request authorization succeeded!");
            center.delegate = self;

        }
    }];


    //Local Notification
    UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
    content.title = @"我是标题";
    content.subtitle = @"呵呵哈哈";
    content.body = @"啦啦啦啦啦啦啦";
    content.badge = @1;
    //设置传递的参数
    content.userInfo = @{@"name":@"小明",@"age":@18};
    //2 分钟后提醒
    //如果repeats 重复 必须60秒以上
    UNTimeIntervalNotificationTrigger *trigger1 = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:120 repeats:NO];

    //每小时重复 1 次喊我喝水
//    UNTimeIntervalNotificationTrigger *trigger2 = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:2600 repeats:YES];
//
//    //每周一早上 8:00 提醒我给老婆做早饭
//    NSDateComponents *components = [[NSDateComponents alloc] init];
//    components.weekday = 2;
//    components.hour = 8;
//    UNCalendarNotificationTrigger *trigger3 = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:components repeats:YES];


    NSString *requestIdentifier = @"sampleRequest";
    UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:requestIdentifier
                                                                          content:content
                                                                          trigger:trigger1];


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

        NSLog(@"添加完成:%@",error);

    }];

}

将要发出通知的时候,还有一个代理方法可以使用

- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler{

    NSDictionary *userInfo = notification.request.content.userInfo;

    NSString *name = userInfo[@"name"];
    NSNumber *age = userInfo[@"age"];

    NSLog(@"%@  %@",name,age);

}

实现效果如下:
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值