iOS10 本地通知

用户通知根据通知信息的来源,可以分为本地通知和远程通知。本地通知是由iOS操作系统根据条件在本机上触发的,例如闹钟就是基于时间触发提醒通知的。远程通知是第三方远程推送给用户的iOS设备的。这种通知常用于商家推销自家的产品。

下面主要讲述开发本地通知的过程:

  1. 请求授权
  2. 通知创建和发送
  3. 通知接收后的处理

请求授权

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:(UNAuthorizationOptionAlert|UNAuthorizationOptionBadge|UNAuthorizationOptionSound) completionHandler:^(BOOL granted,NSError* _Nullable error){
        if(!error)
        {
            NSLog(@"授权成功");
        }
        else
        {
            NSLog(@"授权失败");
        }
    }];
    return YES;
}

通知的创建和发送

  1. 创建通知内容
  2. 创建触发器
  3. 创建通知请求对象
  4. 添加通知请求到通知中心
创建通知内容

用户通知内容由 UNNotificationContent 和 UNMutableNotificationContent 两个类描述,其中前者是不可变的,后面是可变的用户通知内容类,主要的属性如下:
- title : 通知主标题
- subtitle : 通知副标题
- body : 通知内容
- badge : 应用图标标记数字,NSNumber 类型
- sound : 通知到达的音效,UNNotificationSound 类型
- attachments : 通知内容附件集合,可以保存多个 UNNotificationAttachment 对象

视图控制器 ViewController.mscheduleNotification: 方法的代码如下:

UNMutableNotificationContent* content = [[UNMutableNotificationContent alloc] init];
    content.body = @"通知内容:大家好,新年快乐!";
    content.sound = [UNNotificationSound defaultSound];
    content.subtitle = @"通知副标题";
    content.title = @"通知标题";
创建触发器

本地通知触发器有三种类型:时间中断触发器、日历触发器和位置触发器。

这里主要讲一下时间中断触发器:

通过UNTimeIntervalNotificationTrigger 类的静态工厂方法 + triggerWithTimeInterval:repeats: 创建并初始化 UNTimeIntervalNotificationTrigger 对象,在 ViewController.mcreateTimeTrigger 方法的代码如下:

- (UNTimeIntervalNotificationTrigger*)createTimeTrigger
{
    UNTimeIntervalNotificationTrigger* trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:10 repeats:NO];
    return trigger;
}
创建通知请求对象

我们需要将前面创建的内容和触发器封装到通知请求对象 UNNotificationRequest 中,其中通知请求对象用于管理本地通知内容和触发计划。在 ViewController.mscheduleNotification: 方法的代码如下:

UNNotificationRequest* request = [UNNotificationRequest requestWithIdentifier:@"TimeTrigger" content:content trigger:[self createTimeTrigger]];//创建触发器
添加通知请求到通知中心

事实上,现在我们还没有开启通知计划,这需要将通知添加到通知中心。在 ViewController.mscheduleNotification: 方法的代码如下:

[[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError* _Nullable error){
        if(!error)
        {
            NSLog(@"添加到通知中心");
        }
    }];

上述代码将通知对象添加到通知中心,这样就开启了通知计划,等到触发器条件满足时发送通知。

通知接收后的处理

用户接收到通知后,有可能对通知进行一些操作,其中会涉及到的类有:UNNotificationActionUNNotificationCategory,以及委托协议 UNUserNotificationCenterDelegate ,下面简单说明一下:
- UNNotificationAction : 是动作,封装了用户进行的操作。它还有一个子类 UNTextInputNotificationAction ,该子类能够接收用户输入文本操作
- UNNotificationCategory : 是类别,它定义了一组操作
- UNUserNotificationCenterDelegate : 是响应动作的委托对象方法,当用户接收到通知并且执行了操作后,会回调该委托对象方法。

content.categoryIdentifier = @"myCategory";

UNNotificationAction* action = [UNNotificationAction actionWithIdentifier:@"myAction" title:@"点赞" options:UNNotificationActionOptionNone];

UNNotificationCategory* category = [UNNotificationCategory categoryWithIdentifier:@"myCategory" actions:@[action] intentIdentifiers:@[@""] options:UNNotificationCategoryOptionNone];

NSSet* categories = [NSSet setWithObject:category];
[[UNUserNotificationCenter currentNotificationCenter] setNotificationCategories:categories];
[UNUserNotificationCenter currentNotificationCenter].delegate = self;

实现 UNUserNotificationCenterDelegate 协议方法:

- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler
{
    if([response.actionIdentifier isEqualToString:@"myAction"])
    {
        NSLog(@"点赞了");
    }
    completionHandler();
}

注意

  • 通知能够显示在通知栏中显示的前提是,接收通知的应用处于非活动状态,也就是应用退到后台。所以在测试上面的案例时,当点击开启按钮后,需要点击设备的Home键退出应用

大大的坑点

由于我之前设置的触发器的时间间隔是3秒,退出app后始终没有发送本地通知。将时间间隔设置为10秒后,就有效果了,这一点原因还不知道。

项目地址


转载自 关东升的《iOS开发指南》

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值