iOS Notification Service Extension

iOS10之后的通知具有通知扩展功能,可以在系统受到通知、展示通知时做一些事情。

UNNotificationServiceExtension:通知服务扩展,是在收到通知后,展示通知前,做一些事情的。

1. 创建一个UNNotificationServiceExtension
![img](https:////upload-images.jianshu.io/upload_images/12673226-42ddf140254c9485.png?imageMogr2/auto-orient/strip imageView2/2/w/730/format/webp)

如图创建完可以看到工程中多出一个文件

![img](https:////upload-images.jianshu.io/upload_images/12673226-7ca143bc9b4588a2.png?imageMogr2/auto-orient/strip imageView2/2/w/274/format/webp)

在NotificationService.m文件中,有两个自动生成的方法:

// 系统接到通知后,有最多30秒在这里重写通知内容(在此方法可进行一些网络请求,如上报是否收到通知等操作)
- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent *contentToDeliver))contentHandler;
// 处理过程超时,则收到的通知直接展示出来
- (void)serviceExtensionTimeWillExpire;

示例代码:

- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler {
    self.contentHandler = contentHandler;
    self.bestAttemptContent = [request.content mutableCopy];
    NSLog(@"收到推送消息,可进行处理");
//      这里重写一些东西
    self.bestAttemptContent.title = @"这里是标题";    
    self.bestAttemptContent.subtitle = @"这里是子标题";
    self.bestAttemptContent.body = @"这里是body";
    self.contentHandler(self.bestAttemptContent);
}

效果图:

![img](https:////upload-images.jianshu.io/upload_images/12673226-5c7df59516cbbd4a.png?imageMogr2/auto-orient/strip imageView2/2/w/430/format/webp)
  • 如果,你按照教程给自己的app设置好 Notification Service Extension,但是你的扩展程序没有执行。有可能是远程通知的格式问题。 远程推送消息的格式,如下。
{
  "aps":{
        "category":"SECRET",
        "mutable-content": 1,
        "alert":{
           "title":"Secret Message!",
           "body":"(Encrypted)"
         },
    }
}

其中 mutable-content字段不能没有,没有的话,就不会走扩展程序的代码; 其中alert字段的值不能没有,没有的话,就不会走扩展程序的代码。

  • 最重要最容易忽略的一点,必须要设置target的 Deployment Target

    ![img](https:////upload-images.jianshu.io/upload_images/12673226-40b52133b26b865e.png?imageMogr2/auto-orient/strip imageView2/2/w/658/format/webp)

UNNotificationContentExtension:通知内容扩展,是在展示通知时展示一个自定义的用户界面

1. 创建一个UNNotificationContentExtension
![img](https:////upload-images.jianshu.io/upload_images/12673226-078e4ced7a08ef4d.png?imageMogr2/auto-orient/strip imageView2/2/w/730/format/webp)

如图创建完可以看到工程中多出一个文件

![img](https:////upload-images.jianshu.io/upload_images/12673226-f2703402a9b58e5d.png?imageMogr2/auto-orient/strip imageView2/2/w/270/format/webp)

设置Info.plist

![img](https:////upload-images.jianshu.io/upload_images/12673226-57f7550d2897f1f8.png?imageMogr2/auto-orient/strip imageView2/2/w/653/format/webp)

使用的时候,我们参照如下代码:

- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler {
    self.contentHandler = contentHandler;
    self.bestAttemptContent = [request.content mutableCopy];
    NSLog(@"收到推送消息,可进行处理");
//      这里重写一些东西
        self.bestAttemptContent.title = @"这里是标题";
        self.bestAttemptContent.subtitle = @"这里是子标题";
        self.bestAttemptContent.body = @"这里是body";
    // !!!!! 这里是重点!!!!!!!!!!!!
    // 我在这里写死了myNotificationCategory,其实在收到系统推送时,每一个推送内容最好带上一个catagory,跟服务器约定好了,这样方便我们根据categoryIdentifier来自定义不同类型的视图,以及action
    //myNotificationCategory这个值要跟info.plist里面的值一样
    self.bestAttemptContent.categoryIdentifier = @"myNotificationCategory";
    self.contentHandler(self.bestAttemptContent);
}

这里为了方便,我在ContentExtension文件夹下直接更改MainInterface.storyboard文件实现自定义界面

![img](https:////upload-images.jianshu.io/upload_images/12673226-a243c481205d1bde.png?imageMogr2/auto-orient/strip imageView2/2/w/840/format/webp)

运行效果图

![img](https:////upload-images.jianshu.io/upload_images/12673226-c9aaea78437a5604.png?imageMogr2/auto-orient/strip imageView2/2/w/430/format/webp)

附:以上两个项目运行要记得选择项目,否则是无效的。 如图:

![img](https:////upload-images.jianshu.io/upload_images/12673226-b5bfcf2fcb8859c9.png?imageMogr2/auto-orient/strip imageView2/2/w/380/format/webp)
![img](https:////upload-images.jianshu.io/upload_images/12673226-38c9904953657bee.png?imageMogr2/auto-orient/strip imageView2/2/w/433/format/webp)

huhansome云服务器和云数据库推荐,阿里云服务器限时折扣:

阿里云服务器,限时秒杀价低至102/年,券后96/年

阿里云企业级服务器1折限时特惠

阿里云建站 速成美站 - 千套模板,价格低至500元

阿里云SaaS商标注册服务低至680元

CDN爆款产品低至5.5折


腾讯云服务器限时秒杀

腾讯云境外服务器低至2折

腾讯云数据库

最近的文章

干货;新手快速优化关键词排名!【视频教程】

[干货](http://www.yi09.cn/tags/ganhuo/);新手快速优化关键词排名!【[视频](http://www.yi09.cn/tags/shipin/)[教程](http://www.yi09.cn/tags/%E6%95%99%E7%A8%8B/)】网站建设的目的事让人看到,然后一点要有好的排名。每个网站都要经过SEO搜索引擎优化。而关键词的优化尤为重要..**网赚,网赚项目,互联网赚钱,手机赚钱,网上赚钱,一零九网赚博客一零九网赚博客每天给大家分享各种网...…

赚钱致富兼职网赚手机赚继续阅读
更早的文章

明灯副业三十六条小妙招之第19招全自动挂机月入上万躺赚互联网

明灯[副业](http://www.yi09.cn/tags/%E5%89%AF%E4%B8%9A/)三十六条小妙招之第19招全自动[挂机](http://www.yi09.cn/tags/%E6%8C%82%E6%9C%BA/)月入上万[躺赚](http://www.yi09.cn/tags/%E8%BA%BA%E8%B5%9A/)[互联网](http://www.yi09.cn/tags/%E4%BA%92%E8%81%94%E7%BD%91/)**网赚,网赚项目,互联网赚钱,...…

赚钱致富兼职网赚手机赚继续阅读