ios本地通知

一.通知
1.通知的用途:
(1)不在同一页面或者关联性不大的页面之间传递消息
(2)一个页面或者动作需要向多个页面或者对象发送消息后进行处理的情况
(3)通知可以发送者和接受者的一对一的通信,但主要用于某个对象向多个非特定的多个对象发送消息的情况
(4)实际场景:

2.通知的分类:
(1)本地通知:由APP发送到当前设备,不需要网络支持的
(2)远程通知:由APP的服务器发送到苹果的 APNs 服务器,苹果的 APNs 服务器转发到APP相应指定的设备上,需要网络支持
二.三种常见的通知方式:
1.NSNofiticationCenter:内部本质是一个广播的机制,即内部提供一个通知中心,将所需要接受到消息的对象都注册在通知对象(观察者),当某个对象发送通知时,根据通知名来区分不同的通知,所注册在通知中心的对象都会收到这个通知,观察者是通过通知名来区分是否是来接受这个通知。
(1)过程:

//1.创建通知对象:
//name:用来标识通知的名字
//object:对象,和通知一起发送的信息中的对象,可以用来传递信息,可为nil
//userInfo:NSDictionary字典类型,为来传递和通知相关的各种信息而使用的字典,可为nil
NSNotification *notification = [[NSNotification alloc] initWithName:@"deleteTarget" object:self.target.name userInfo:nil];
//2.通知中心:在使用NSNotificationCenter的类接口中实现,但一般每个进程中都会有一个通知中心,不需要自己创建
+(id) defaultCenter //获取通知中心
//3.发送通知
[[NSNotificationCenter defaultCenter] postNotification:notification];
//4.在通知中心中注册观察者
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceiveDeleteTargetNotification:) name:@"deleteTarget" object:nil];
//5.取消观察者的注册
- (void)dealloc{
   //从默认的通知中心中删除掉对象obj的所有观察者
    [[NSNotificationCenter defaultCenter] removeObserver:obj];
}

(2)通知队列:NSNotificationQueue
A.通知队列是将所有通知对象都存在一个队列(先进先出),然后按照顺序向通知中心发送消息
B.两种通知操作:
异步发送:先将通知添加到队列中,当处理完当前操作后,在按照顺序依次处理队列中的通知
合并相同的通知:将通知中心中相同的通知进行合并,从而使处理可以变得更加高效,合并的条件是通知名或者通知源相同时就可以合并为一个进行处理
2.User Nofications:用户通知,手机开启页面中APP通知的消息或者APP右上角角标显示的未读消息称为用户通知
(1)过程:
ios10系统以后采用了新的类库UNUserNotificationCenter

//1.注册通知:AppDelegate.m的application: didFinishLaunchingWithOptions:方法中
//用户是否授权开启通知功能
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];//注册通知
    [center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionAlert | UNAuthorizationOptionSound) completionHandler:^(BOOL granted,NSError * _Nullable error){
         //获取用户是否同意开启通知
        if(granted){
            NSLog(@"获取授权成功");
        }
    }]; //请求获取通知权限(角标,弹窗,声音)
    return YES;
}
//2.初始化通知:发送本地通知
- (void)regiterLocalNotification{
    NSDateComponents * components = [[NSDateComponents alloc] init];
    for(int i=0;i<self.targetName.count;i++)
    {
        self.target = [[KSDataStored shareManager] queryTargetWithName:self.targetName[i]];
        NSLog(@"%@",self.target.beginTime);
         components = [[NSDateComponents alloc] init];
         components = [[NSCalendar currentCalendar]
                                         components:NSCalendarUnitYear|
                                         NSCalendarUnitMonth|
                                         NSCalendarUnitDay |
                                         NSCalendarUnitHour |
                                         NSCalendarUnitMinute |
                                         NSCalendarUnitSecond
                                         fromDate:self.target.beginTime];
        NSLog(@"components:%@",components);
        //(1)注册通知
        UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
        center.delegate = self;
        //(2)设置通知体:设置通知的标题title,副标题subtitle,内容body,声音sound,角标badge
        UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
       content.title = [NSString localizedUserNotificationStringForKey:@"更新目标状态" arguments:nil];
       content.body = [NSString localizedUserNotificationStringForKey:[NSString stringWithFormat:@"目标名字:%@",self.targetName[i]] arguments:nil];
       content.sound = [UNNotificationSound defaultSound];
       content.badge = @1;
    //(3)设置通知触发事件:
    //A.用时间间隔,如果重复必须>60s
    UNTimeIntervalNotificationTrigger *trigger1 = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:60 repeats:YES];
    //B.日期触发器
    UNCalendarNotificationTrigger *trigger2 = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:components repeats:NO];
    
    //(4)创建通知请求对象 UNNotificationRequest
    UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"收到回复" content:content trigger:trigger2];
   
    //(5)将通知添加到通知中心
    [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error){
        NSLog(@"Error:%@",error);
    }];     
    }
}
//3.实现通知的代理方法:UNUserNotificationCenterDelegate
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler{
    //需要执行这个方法,选择是否提醒用户,有Badege,sound,alert三种类型可以设置
    completionHandler
    (UNNotificationPresentationOptionBadge | UNNotificationPresentationOptionAlert | UNAuthorizationOptionSound);
}

3.KVO Notification:是被观察者直接发送消息给观察者,是对象间的交互,不需要通知中心,属于直接的交互,观察者A监听被观察者B的某个属性,当B的属性发生更改时,A就会收到通知,执行相应的方法。
实现原理:基本的原理:当观察某对象A时,KVO机制动态创建一个对象A当前类的子类,并为这个新的子类重写了被观察属性keyPath的setter 方法。setter 方法随后负责通知观察对象属性的改变状况。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值