iOS 10推送通知有哪些新功能?

[vc_row full_width =”“ parallax =”“ parallax_image =”“ bg_type =”“ parallax_style =”“ bg_image_new =”“ layer_image =”“ bg_image_repeat =”“ bg_image_size =”“ bg_c​​stm_size =”“ bg_img_attach =”“ parallax_style =”“ bg_image_posiiton =”“ animation_direction =”“ animation_repeat =”“ video_url =”“ video_url_2 =”“ u_video_url =”“ video_opts =”“ video_poster =”“ u_start_time =”“ u_stop_time =”“ viewport_vdo =”“ enable_controls =”“ bg_override = ”” disable_on_mobile_img_parallax =”” parallax_content =”” parallax_content_sense =”” fadeout_row =”” fadeout_start_effect =”” enable_overlay =”” overlay_color =”” overlay_pattern =”” overlay_pattern_opacity =”“ overlay_pattern_size =”” lay_pattern_attach“ multi_color_overlay_opacity =”” seperator_enable =”” seperator_type =”” seperator_position =”” seperator_shape_size =”” seperator_svg_height = ” seperator_shape_background =”” seperator_shape_border =”” seperator_shape_border_color =”” seperator_shape_border_width =”” icon_type =”” icon =”” icon_size =”” icon_color =”” icon_style =”” icon_color_bg =“” icon_border_style =”” icon_order_b =”“ icon_border_radius =”“ icon_border_spacing =”“ icon_img =”“ img_width =”“ ult_hide_row =”“ ult_hide_row_large_screen =”“ ult_hide_row_desktop =”“ ult_hide_row_tablet =”“ ult_hide_row_row_table_” mobile_hide_row_row_table_“宽度=“ 1/1”] [vc_column_text css =”。vc_custom_1479987606117 {margin-bottom:30px!important;}”]

点击下载完整源代码

  • iOS: 10
  • 语言:斯威夫特
  • Xcode版本: 8
  • Mac OS: 10.12

[/ vc_column_text] [vc_column_text css =”。vc_custom_1479970234416 {margin-bottom:20px!important;}”

步骤1:创建简单的Xcode项目

对于此演示,请首先通过选择“单视图应用程序”类型来创建Xcode项目。[/ vc_column_text] [vc_column_text css =”。vc_custom_1479970250772 {margin-bottom:20px!important;}”]

步骤2:启用远程通知

  • 现在打开您的应用程序目标。 选择功能,并确保已启用“推送通知”。 为此,请首先打开此屏幕。
  • 现在,我们需要在后台模式下选择“远程通知”。 为此,请首先打开此屏幕。

[/ vc_column_text] [vc_column_text css =”。vc_custom_1479970266805 {margin-bottom:20px!important;}”

步骤3:注册您的应用程序以进行远程通知

  • 现在打开AppDelegate.swift。 为此,请先打开此屏幕
  • 导入“ UserNotifications”并在“ didFinishLaunchingWithOptions”方法中编写以下代码
  • 让中心= UNUserNotificationCenter.current()
    center.requestAuthorization(options:[.alert,.sound]){(已授予,错误)在
    //根据通知是否被授权进行的操作
    防护错误==无其他{
    //显示错误..处理错误..等
    返回
    }
    如果授予{
    //在这里做东西..
    }
    其他{
    //处理用户拒绝权限
    }
    }
    application.registerForRemoteNotifications()
  • 现在您已完成注册远程通知。

[/ vc_column_text] [vc_column_text css =”。vc_custom_1479970284759 {margin-bottom:20px!important;}”

步骤4:添加Notification Service扩展

  • 现在,我们需要在应用程序中添加“ Notification Service Extension”。 为此,请先打开此屏幕
  • 在您的应用程序中选择“ Notification Service Extension”,然后单击“ Next”按钮
  • 输入扩展名。 在这里,我们输入了“服务扩展”。 然后点击“完成”按钮。
  • 一旦您单击“完成”按钮,用户将能够看到以下屏幕。
  • 在此屏幕上,Xcode要求您为该新扩展名激活方案。 因此,您可以使用此方案进行构建和调试。 可以在工具栏或“产品”菜单中选择方案。 请激活“服务扩展”方案。
  • 现在,您已经完成了添加通知服务扩展的操作。

[/ vc_column_text] [vc_column_text css =”。vc_custom_1479970306035 {margin-bottom:20px!important;}”

步骤5:了解添加Notification Service Extension后获得的文件

在此扩展中,您将能够找到总共两个文件:

  • 信息清单
  • NotificationService.swift

信息清单:

  • 在plist文件中,您必须提及要使用Notification Service Extension执行的操作类型。

NotificationService.swift
在swift文件中,您必须根据Notification Service Extension的受支持类型实施您要处理的所有操作。

  • 您将看到几个委托方法-一个用于接收通知请求,另一个用于处理服务扩展的到期。
  • 现在重点介绍这种“ didReceiveRequestWithContentHandler”方法。
  • 在“ didReceiveRequestWithContentHandler”方法中编写以下代码。
 如果让bestAttemptContent = bestAttemptContent { 
//在此处修改通知内容...
  //从通知有效内容中获取自定义数据 
如果让notificationData = bestAttemptContent.userInfo [“ data”]为? [String:String] {
  //抓取附件 
如果让urlString = notificationData [“ attachment-url”],则让fileUrl = URL(string:urlString){
  //下载附件 
URLSession.shared.downloadTask(with:fileUrl){(位置,响应,错误)在
如果让location = location {
  //移动临时文件以删除.tmp扩展名 
让tmpDirectory = NSTemporaryDirectory()
让tmpFile =“ file:”。appending(tmpDirectory).appending(fileUrl.lastPathComponent)
让tmpUrl = URL(string:tmpFile)!
尝试! FileManager.default.moveItem(位于:位置,至:tmpUrl)
//将附件添加到通知内容
如果让附件=尝试? UNNotificationAttachment(identifier:“”,url:tmpUrl){
self.bestAttemptContent?.attachments = [attachment]
}
}
  //提供通知内容 
self.contentHandler!(self.bestAttemptContent!)
}。恢复()
}
}
}
  • 似乎这里发生了很多事情,但这确实很简单。 最初,我们正在探索包含关键字“ attachment-url”的名为“ data”的词典的通知有效负载。 如果此密钥存在,我们将通过密钥值中指定的URL下载媒体。
  • URLSession将媒体下载到临时存储中,并附加一个.tmp文件扩展名(我们需要删除该扩展名),以便应用程序可以推断文件类型并显示它。 为此,我们需要将文件移动到应用程序的本地文件管理器,仅此而已-我们的媒体已准备好附加到通知内容,并提供给用户。
  • 现在重点介绍这种“ serviceExtensionTimeWillExpire”方法。
  • 这在扩展即将被系统终止之前调用
  • 以此为契机,对修改后的内容进行“最佳尝试”; 否则,将使用原始的推送有效负载。
  • 现在,您已经完成了通知服务丰富的通知。
  [/ vc_column_text] [vc_column_text css =“。vc_custom_1479970325722 {margin-bottom:20px!important;}”] 

步骤6:注册设备令牌以进行远程通知

  • 现在打开AppDelegate.swift。 为此,请先打开此屏幕
  • 创建用于将数据获取到hexString的数据扩展名。
 扩展数据{ 
func hexString()->字符串{
return self.reduce(“”){字符串,字节在
字符串+字符串(格式:“%02X”,字节)
}
}
}
  • 在“ didRegisterForRemoteNotificationsWithDeviceToken”方法中编写代码以获取设备令牌
  func application(_ application:UIApplication,didRegisterForRemoteNotificationsWithDeviceToken deviceToken:Data){ 
让deviceTokenString = deviceToken.hexString()
print(“ deviceToken:\(deviceTokenString)”)
}
  • 使用设备令牌发送推送通知。
  [/ vc_column_text] [vc_column_text css =“。vc_custom_1479970343892 {margin-bottom:20px!important;}” 

步骤7:在iOS装置上执行

  • 现在,将设备与系统连接。
  • 从方案中选择“ NotificationDemo”并运行。 在设备上运行后,请停止它。
  [/ vc_column_text] [vc_column_text css =“。vc_custom_1479970362281 {margin-bottom:20px!important;}”] 

步骤8:如何测试

  • 我们提到服务扩展仅拦截了某些通知。 那么,iOS如何知道要拦截哪些通知? 好吧,您只需在有效负载中添加一个可变内容标签并将其设置为1。
  • 您需要做的最后一件事是将媒体附件URL添加到我们自己称为“数据”的字典中的有效负载中,但是您可以为其命名。 只需在字典中添加一个键值对,只要您在服务扩展中使用的键与此处的键相同,就可以完成!
  • 以下是有效负载示例,
  { 
“ aps”:{
“警告”:{
“ title”:“ Pokemon Go”,
“ subtitle”:“ iOS 10支持!”,
“ body”:“现在将更多内容添加到您的推送通知中!”
},
“可变内容”:1
},
“数据”:{
“附件网址”:“ https://psprovocative.com/wp-content/uploads/2016/08/pokemon-go-heart-pokestopjpg-d1c6dba768e27a23-300x300.jpg”
}
}
 您现在可以在iOS 10推送通知中发送图像,视频,音频和GIF! 只需更改URL即可指向另一个媒体文件,然后亲自查看结果。 
  1. 纯文本
  1. 图片附件
  1. GIF附件
  [/ vc_column_text] [vc_column_text css =“。vc_custom_1479970381879 {margin-top:30px!important; padding-top:15px!important; padding-right:15px!important; padding-bottom:15px!important; padding-left:15px!重要; background-color:#f2f2f2!important;}“] 

更多参考

  1. 用户通知
  2. UNNotificationServiceExtension
  3. 联合国通知附件
  [/ vc_column_text] [/ vc_column] [/ vc_row] [vc_row video_opts =“” multi_color_overlay =“” css =“。vc_custom_1475132846899 {margin-top:20px!important;}”] [vc_column width =“ 1/1”] [ vc_row_inner] [vc_column_inner el_class =“” width =“ 1/1”] [vc_raw_html] == 
  [/ vc_raw_html] [/ vc_column_inner] [/ vc_row_inner] [/ vc_column] [/ vc_row]