iOS 10 UserNotifications 实践

简介

在 iOS 10 中新加入 UserNotifications 框架,对以往杂乱无章的通知系统 API 进行了统一,更方便开发者们快速引用。关于新框架的一些基本概念在喵大《iOS 10 UserNotifications 框架解析》中已有详细的描述,本文只对实践中的具体运用做介绍。

基本流程

iOS 10中通知相关操作遵循下面的流程:


流程.png

权限申请

iOS 各个版本 Notifications 权限申请代码如下:

// iOS 10 support
if #available(iOS 10.0, *) {
    let options: UNAuthorizationOptions = [.alert, .sound, .badge]
    UNUserNotificationCenter.current().requestAuthorization(options: options) { granted, error in
        if granted {
           // 用户允许进行通知
        }
    }
}
// iOS 9 support
else if #available(iOS 9, *) {
    let types: UIUserNotificationType = [.alert, .sound, .badge]
    let settings = UIUserNotificationSettings(types: types, categories: nil)
    UIApplication.shared.registerUserNotificationSettings(settings)
    // ...其他操作
    if UIApplication.shared.currentUserNotificationSettings?.types != [] {
        // 用户允许进行通知
    }
} 
// iOS 8 support
else if #available(iOS 8, *) {
    let types: UIUserNotificationType = [.alert, .sound, .badge]
    let settings = UIUserNotificationSettings(types: types, categories: nil)
    UIApplication.shared.registerUserNotificationSettings(settings)
    // ...其他操作
    if UIApplication.shared.currentUserNotificationSettings?.types != [] {
        // 用户允许进行通知
    }
} 
// iOS 7 support
else {
    let types: UIRemoteNotificationType = [.badge, .sound, .alert]
    UIApplication.shared.registerForRemoteNotifications(matching: types)
}

注册Token

当用户同意授权以后,还需要向系统注册一个 Device Token,并将这个 token 发送到 APNs(Apple Push Notification Service),然后 APNs 通过 token 识别设备和应用,并通知推送给用户。

// 向 APNs 请求 token
UIApplication.shared.registerForRemoteNotifications()

// Called when APNs has assigned the device a unique token
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    let tokenString = deviceToken.reduce("", {$0 + String(format: "%02X", $1)})
    print("APNs device token: \(tokenString)")
    // 上传至后台服务器
}

// Called when APNs failed to register the device for push notifications
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {  
    // Print the error to console (you should alert the user that registration failed)
    print("APNs registration failed: \(error)")
}

发送推送通知

关于 APNs 的推送原理 就不做详细说明,这里以 Node.js 为例子搭建推送测试工具,步骤如下:
1、创建 APNs 服务的访问 Auth Key ID

创建key.png

2、获取 Key ID,点击 Download 同时下载 .p8 证书文件
获取key.png

3、获取开发者账号的 Team ID
Membership.png

4、编写测试脚本 push.js,内容如下:

var apn = require('apn');

if (process.argv.length == 3) {
    // Enter the device token
    var deviceToken = process.argv[2];

    // Set up apn with the APNs Auth Key
    var apnProvider = new apn.Provider({  
         token: {
            key: 'xxxx.p8', // Path to the key p8 file
            keyId: 'xxxx',
            teamId: 'xxxx',
        },
        production: false // Set to true if sending a notification to a production iOS app
    });

    // Prepare a new notification
    var notification = new apn.Notification();

    // Specify your iOS app's Bundle ID (accessible within the project editor)
    notification.topic = 'com.domain.xxxx';

    // Set expiration to 1 hour from now (in case device is offline)
    notification.expiry = Math.floor(Date.now() / 1000) + 3600;

    // Set app badge indicator
    notification.badge = 1;

    // Play ping.aiff sound when the notification is received
    notification.sound = 'default';

    // Display the following message (the actual notification text, supports emoji)
    notification.alert = 'Hello World \u270C';

    // Send any extra payload data with the notification which will be accessible to your app in didReceiveRemoteNotification
    notification.payload = {id: 123};

    // Actually send the notification
    apnProvider.send(notification, deviceToken).then(function(result) {  
        // Check the result for any failed devices
        console.log(result);
    });
} else {
    console.log("Usage: node push.js <deviceToken>");
}

至此就可以测试远程推送通知了。当然还可以其他类似的工具 NWPusher

node push.js 8c6f8b7056613a5223c690fb171697c524173b9c39e7dff688c66f0b23fbefdb

展示处理

iOS 10以前通知完全是系统行为,开发者无法自主控制,引入 UserNotifications 框架以后通知数据处理流程如下:

Notification Extension.png

其中 Service Extension 和 Content Extension 前者可以让我们有机会在收到远程推送的通知后,展示之前对通知内容进行修改;后者可以用来自定义通知视图UI的样式。尤其是Service Extension收到通知以后必执行,iOS平台终于可以做更精准的推送到达率统计了
1、创建 Service Extension,Xcode 会自动生成模板代码。在这里可以进行埋点统计,并在通知中展示多媒体文件(图片/音频/视频)。

class NotificationService: UNNotificationServiceExtension {

    var contentHandler: ((UNNotificationContent) -> Void)?
    var bestAttemptContent: UNMutableNotificationContent?

    /**
     该方法可以在限定时间内(30秒)修改请求中的 content 内容,然后返回给系统显示
     */
    override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
        self.contentHandler = contentHandler
        bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
        
        if let bestAttemptContent = bestAttemptContent {
            // 进行 Push 到达的埋点统计
            var msgid = 1
            if let tmp = bestAttemptContent.userInfo["msgid"] as? NSNumber {
                msgid = tmp.intValue
            }
            tracePush(msgId: msgId)

            /** 
                添加多媒体附件
                内置资源支持10MB以内图片,50M以内音视频
                外链支持30秒内能下载完成的多媒体文件
             */ 
            if let imageURLString = bestAttemptContent.userInfo["image"] as? String, let URL = URL(string: imageURLString) {
                downloadAndSave(url: URL) { localURL in
                    if let localURL = localURL {
                       do {
                          let attachment = try UNNotificationAttachment(identifier: "image_downloaded", url: localURL, options: nil)
                          bestAttemptContent.attachments = [attachment]
                       } catch {
                          print(error)
                       }
                    }
                    contentHandler(bestAttemptContent)
                }
            } else {
                contentHandler(bestAttemptContent)
            }
        }
    }

    /**
     一定时间内(30秒)没将内容返回给系统,则会自动调用该方法,未完成的修改将被忽略
     */
    override func serviceExtensionTimeWillExpire() {
        // Called just before the extension will be terminated by the system.
        // Use this as an opportunity to deliver your "best attempt" at modified content, otherwise the original push payload will be used.
        if let contentHandler = contentHandler, let bestAttemptContent =  bestAttemptContent {
            contentHandler(bestAttemptContent)
        }
    }
}

2、创建 Content Extension,Xcode 会自动生成模板代码(若不需要自定义UI,可跳过本部分)。iOS 10中通知分类注册方式更加简洁,通知响应处理方式也集中到了独立的 delegate中,包括本地和远程通知的处理。

func registerNotificationCategory() {
    if #available(iOS 10.0, *) {
       UNUserNotificationCenter.current().setNotificationCategories(createiOS10Category())
       UNUserNotificationCenter.current().delegate = notificationHandler
    } else {
       let types: UIUserNotificationType = [.alert, .sound, .badge]
       let settings = UIUserNotificationSettings(types: types, categories: createiOS89Category())
       UIApplication.shared.registerUserNotificationSettings(settings)
    }
}

// 当 application 处于前台活跃状态时会被调用,控制是否需要弹出提示
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
    completionHandler([.alert, .sound, .badge])
}

// 当用户点击通知启动 application,前台活跃状态点击通知时都会被调用到
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
    completionHandler()
}

Content Extension 也可以在 application 未启动前,处理已注册过的通知分类

// 用户每收到一条通知都会执行一次调用
func didReceive(_ notification: UNNotification)  {

}

// 用户点击通知时会调用
func didReceive(_ response: UNNotificationResponse, completionHandler completion: @escaping (UNNotificationContentExtensionResponseOption) -> Swift.Void) {

}

3、编辑推送信息,简单的示例 payload 如下:

{
  "msgid": 100,
  "aps":{
    "alert":{
      "title":"Image Notification",
      "body":"Show me an image from web!"
    },
    "mutable-content":1
  },
  "image": "https://onevcat.com/assets/images/background-cover.jpg"
}

详细定义参见 苹果官方文档
4、Service Extension 和 Content Extension 都有独立的 Bundle ID,打包时需要进行签名,同时也需要配置 ATS

ATS.png

注意事项:

1、mutable-content 表示接收到通知时会对内容进行修改,必须配置为1,否则不会执行 Service Extension。
2、附件可以设置多个 attachment 实例,但系统默认只会显示第一个,当然可以通告代码修改它们的顺序,以显示最符合情景的图片或者视频。
3、extension 的 bundle 和 app main bundle 并不相同,属于不同的沙盒目录,也就是说当要使用内置资源时需添加进 extension 的 bundle 中。
4、如果使用的图片和视频文件不在 bundle 内部,它们将被移动到系统的负责通知的文件夹下,然后在当通知被移除后删除。如果媒体文件在 bundle 内部,它们将被复制到通知文件夹下。每个应用能使用的媒体文件的文件大小总和是有限制,超过限制后创建 attachment 时将抛出异常,即不能同时创建太多的 attachment。
5、当访问一个已经创建好的 attachment 时,需使用startAccessingSecurityScopedResource来获取访问权限:

let content = notification.request.content
if let attachment = content.attachments.first {  
    if attachment.url.startAccessingSecurityScopedResource() {  
       eventImage.image = UIImage(contentsOfFile: attachment.url.path!)
      attachment.url.stopAccessingSecurityScopedResource()  
    }  
}  

关于 Service Extension、Content Extension 和多媒体通知的使用,可以参考 Demo

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 157,298评论 4 360
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 66,701评论 1 290
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 107,078评论 0 237
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,687评论 0 202
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,018评论 3 286
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,410评论 1 211
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,729评论 2 310
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,412评论 0 194
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,124评论 1 239
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,379评论 2 242
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,903评论 1 257
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,268评论 2 251
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,894评论 3 233
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,014评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,770评论 0 192
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,435评论 2 269
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,312评论 2 260

推荐阅读更多精彩内容