玩转 iOS 10 推送 —— UserNotifications Framework(上)

iOS 10 came

在今年 6月14号 苹果开发者大会 WWDC 2016 以后,笔者赶忙就去 apple 的开发者网站下载了最新的 Xcode 8 betaiOS 10 beta,而后在本身的手机上装了 iOS 10 beta ,狠狠地体验了一把。
能够说 iOS 10 不管从界面风格,仍是 Framework 都作了不少改动。最直观的感觉就是界面的圆角增多了,系统动画更加多样和流畅,系统 App 的功能也变得更丰富了。javascript

而 iOS 10 里的推送功能,也较以前更增强大,
今天咱们就来聊聊 iOS 10 里的推送功能。java

Notifications before iOS 10

首先咱们一块儿简单回顾下 iOS 10 之前的推送服务。
iOS 推送分为 Local Notifications(本地推送) 和 Remote Notifications(远程推送),先看 2 张图:ios

Local Notifications

Remote Notifications

简单的说就是本地推送经过 App 本地定制,加入到系统的 Schedule 里,而后在指定的时间推送指定文字。而远程推送经过服务端向苹果推送服务器 Apple Push Notification Service (APNs) 发送 Notification Payload,以后 APNs 再将推送下发到指定设备的 指定 App 上。
以及 iOS 7 以后在不显式地弹窗打扰用户的状况下,进行的静默推送服务器

Silent Push

具体作法能够参考 iOS 7 Background Remote Notificationapp

User Notifications Framework


好,扯了这么多,该进入今天的正题了 —— User Notifications Framework 。
首先在 AppDelegate.m动画

import
#import <UserNotifications/UserNotifications.h>复制代码
注册推送

如下分别是 iOS 10 以前和以后的注册方式,其中的 UNAuthorizationOptions 里还能够找到 1 个 UNAuthorizationOptionCarPlay 的值是专为车载系统定制的值。网站

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    //iOS 10 before
    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil];
    [application registerUserNotificationSettings:settings];

    //iOS 10
    UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
    [center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert) completionHandler:^(BOOL granted, NSError * _Nullable error) {
        if (!error) {
            NSLog(@"request authorization succeeded!");
        }
    }];

    return YES;
}复制代码
Notification settings

以前注册推送服务,ios 8 及以前使用了不一样的 API,而且返回结果也不一样。如今 apple 不只统一了这个 API,并且咱们能够获取到用户更加详细的设定了。spa

[center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
        NSLog(@"%@",settings);
}];复制代码

打印得到以下信息:3d

<UNNotificationSettings: 0x16567310; 
authorizationStatus: Authorized, 
notificationCenterSetting: Enabled, 
soundSetting: Enabled, 
badgeSetting: Enabled, 
lockScreenSetting: Enabled, 
alertSetting: NotSupported,
carPlaySetting: Enabled, 
alertStyle: Banner>复制代码
Token Registration

跟以前同样code

[[UIApplication sharedApplication] registerForRemoteNotifications];复制代码
Content

之前只能展现一条文字,如今能够有 title 、subtitle 以及 body 了。


定制方法以下:

//Local Notification
UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
content.title = @"Introduction to Notifications";
content.subtitle = @"Session 707";
content.body = @"Woah! These new notifications look amazing! Don’t you agree?";
content.badge = @1;

//Remote Notification
{
"aps" : {
    "alert" : { 
         "title" : "Introduction to Notifications", 
         "subtitle" : "Session 707",         
         "body" : "Woah! These new notifications look amazing! Don’t you agree?"
                },
    "badge" : 1
        },
}复制代码
Triggers

又是一个新的功能,有三种

  • UNTimeIntervalNotificationTrigger
  • UNCalendarNotificationTrigger
  • UNLocationNotificationTrigger
//2 分钟后提醒
UNTimeIntervalNotificationTrigger *trigger1 = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:120 repeats:NO];

//每小时重复 1 次喊我喝水
UNTimeIntervalNotificationTrigger *trigger2 = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:3600 repeats:YES];

//每周一早上 8:00 提醒我给老婆作早饭
NSDateComponents *components = [[NSDateComponents alloc] init];
components.weekday = 2;
components.hour = 8;
UNCalendarNotificationTrigger *trigger3 = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:components repeats:YES];

//#import <CoreLocation/CoreLocation.h>
//一到麦当劳就喊我下车
CLRegion *region = [[CLRegion alloc] init];
UNLocationNotificationTrigger *trigger4 = [UNLocationNotificationTrigger triggerWithRegion:region repeats:NO];复制代码
Add Request
NSString *requestIdentifier = @"sampleRequest";
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:requestIdentifier
                                                                          content:content
                                                                          trigger:trigger1];
[center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {

}];复制代码

推送小结

而后整个推送的过程就变成了酱紫:

  • Local Notifications 经过定义 ContentTriggerUNUserNotificationCenter 进行 request 这三部曲来实现。
  • Remote Notifications 则向 APNs 发送 Notification Payload
Notification Handling

设定了推送,而后就结束了?iOS 10 并无这么简单!
经过实现协议,使 App 处于前台时捕捉并处理即将触发的推送:

@interface AppDelegate () <UNUserNotificationCenterDelegate>

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

    completionHandler(UNNotificationPresentationOptionAlert | UNNotificationPresentationOptionSound);

}复制代码

让它只显示 alert 和 sound ,而忽略 badge 。

Notification Management

完全掌控整个推送周期:

  • Local Notification 经过更新 request
  • Remote Notification 经过新的字段 apns-collapse-id

经过以前的 addNotificationRequest: 方法,在 id 不变的状况下从新添加,就能够刷新原有的推送。

NSString *requestIdentifier = @"sampleRequest";
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:requestIdentifier
                                                                      content:newContent
                                                                      trigger:newTrigger1];
[center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {

}];复制代码

删除计划的推送:

[center removePendingNotificationRequestsWithIdentifiers:@[requestIdentifier]];复制代码

此外 UNUserNotificationCenter.h 中还有诸如删除全部推送、查看已经发出的推送、删除已经发出的推送等等强大的接口。

刷新原有的推送后,在通知中心的显示里,也会有相应的变化,这里注意第 2 条信息,如今比分是 1:0


比分刷新后为 1:1,在不产生新的推送条目的状况下位置被前置了!

试想利用这个方法,不断的刷新推送,是否是就能够作到让本身 App 的推送内容始终展现在用户手机通知中心的最顶端,力压其他全部内容了呢?总感受有点不厚道啊~

Advanced Notifications


关于推送的更多相似 Media Attachments 的高级功能,咱们将在下一篇里详细讨论。

Media Attachments

为推送添加更多媒体附件,诸如图片、音乐

继续浏览下一篇 玩转 iOS 10 推送 (中)

相关文章
相关标签/搜索