• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

【Swift】UNNotificationServiceExtension

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

一、简介

  An object that modifies the content of a remote notification before it's delivered to the user.

  是一个能在远程通知传递到用户之前进行修改内容的对象。

二、概述

  A UNNotificationServiceExtension object provides the entry point for a Notification Service app extension, which lets you customize the content of a remote notification before it is delivered to the user. A Notification Service app extension doesn't present any UI of its own. Instead, it is launched on demand when a notification of the appropriate type is delivered to the user’s device. You use this extension to modify the notification’s content or download content related to the extension. For example, you could use the extension to decrypt an encrypted data block or to download images associated with the notification.

  UNNotificationServiceExtension对象提供通知服务应用程序扩展的入口点,允许您在远程通知传递给用户之前对其内容进行自定义。通知服务应用程序扩展本身不提供任何UI,相反,当通知发送到用户设备时,你可以根据需要启动,可以通过通知扩展来修改通知内容或下载关联的扩展内容。例如:你可以使用扩展来加解密数据或下载与通知关联的图像。

  You don't create UNNotificationServiceExtension objects yourself. Instead, the Xcode template for a Notification Service Extension target contains a subclass for you to modify. Use the methods of that subclass to implement your app extension’s behavior. When a remote notification for your app is received, the system loads your extension and calls its didReceive(_:withContentHandler:) method only when both of the following conditions are met:

  你不需要自己创建UNNotificationServiceExtension对象,相反,Xcode模板已经包含了一个通知扩展需要修改的子类,使用子类方法来实现引用程序的扩展行为,当时一个远程通知到应用程序时,系统加载你的扩展并调用didReceive(_:withContentHandler:) 方法,但需要通知满足以下条件:

  The remote notification is configured to display an alert.

  远程推送通知被设置为alert提示
  The remote notification’s aps dictionary includes the mutable-content key with the value set to 1.

  远程通知的aps字典中包含mutable-content键值,而且其值设置为1. 

  The didReceive(_:withContentHandler:) method performs the main work of your extension. You use that method to make any changes to the notification’s content. That method has a limited amount of time to perform its task and execute the provided completion block. If your method does not finish in time, the system calls the serviceExtensionTimeWillExpire() method to give you one last chance to submit your changes. If you don't update the notification content before time expires, the system displays the original content.

   didReceive(_:withContentHandler:)方法执行扩展的主要工作。你可以是使用该方法对通知内容做一些更改,该方法提供了有限的时间去执行任务和提供的数据块,如果你的方法在该时间内没有完成,系统会执行serviceExtensionTimeWillExpire()方法给你最后一次机会来提交你的修改,如果你不能在时间过期之前完成更新通知内容,系统会按照原始内容进行显示。

  As for any app extension, you deliver a Notification Service app extension class as a bundle inside your app. The template provided by Xcode configures the Info.plist file automatically for this app extension type. Specifically, it sets the value of the NSExtensionPointIdentifier key to com.apple.usernotifications.service and sets the value of the NSExtensionPrincipalClass key to the name of your UNNotificationServiceExtensionsubclass.

  对于任何应用程序的扩展,都会将通知服务扩展类交付给应用程序中,对于Xcode中提供的末班,在info.plist文件中都会自动扩展类型。具体说,为通知扩展子类添加两个键值NSExtensionPointIdentifier 和 NSExtensionPrincipalClass

  For information about how to set up and send remote notifications, see Setting Up a Remote Notification Server.

  如何设置和发送远程通知,看链接。

三、Subclassing Notes

  The Xcode templates provide a subclass of UNNotificationServiceExtension for you to modify. You must implement the didReceive(_:withContentHandler:) method and use it to process incoming notifications. It is also strongly recommended that you override the serviceExtensionTimeWillExpire() method.

  Xcode模板为通知扩展提供了子类,你必须实现完成以下两个方法。

func didReceive(UNNotificationRequest, withContentHandler: (UNNotificationContent) -> Void)
    //Asks you to make any needed changes to the notification and notify the system when you're done.
func serviceExtensionTimeWillExpire()
//Tells you that your extension is about to be terminated.

 

  如何创建UNNotificationServiceExtension请自行百度。

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
HandyJSON.Metadata.ClassXcode10.2,swift5.0报错linkercommandfailedwithexitcode1发布时间:2022-07-18
下一篇:
Swift5.4语言指南(一)关于Swift发布时间:2022-07-18
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap