服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - IOS - iOS12新特性之推送通知详解

iOS12新特性之推送通知详解

2021-05-03 16:57FlyOceanFish IOS

这篇文章主要给大家介绍了关于iOS12新特性之推送通知的相关资料文中通过图文以及示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

序言

众所周知,ios中消息推送扮演了不可或缺的位置。不管是本地通知还是远程通知无时不刻的在影响着我们的用户体验,以致于在ios10的时候苹果对推送大规模重构,独立了已 usernotifications 和 usernotificationsui 两个单独的framework,可见重要性一斑。针对于wwdc18苹果又给我们带来了什么惊喜呢?

新特性

  • grouped notifications 推送分组
  • notification content extensions 推送内容扩展中的可交互和动态更改action
  • notification management 推送消息的管理
  • provisional authorization 临时授权
  • critical alerts 警告性质的推送

推送分组

随着手机上应用的增多,尤其qq和微信这两大聊天工具,当手机锁屏的时候,伴随着就是好满屏的推送消息。这一现象不知大家有没有觉着不高效和体验性比较差呢?苹果针对锁屏情况下,对消息进行了分组,从而有效的提高了用户的交互体验,分组形式如下:

iOS12新特性之推送通知详解

分组形式:

  • 苹果会自动帮我们以app的为分类依据进行消息的分组;
  • 如果我们设置了 threadidentifier 属性则以此属性为依据,进行分组。

iOS12新特性之推送通知详解

代码如下:

?
1
2
3
4
let content = unmutablenotificationcontent()
content.title = "notifications team"
content.body = "wwdc session after party"
content.threadidentifier = "notifications-team-chat"//通过这个属性设置分组,如果此属性没有设置则以app为分组依据

摘要(summary)格式定制

当苹果自动将推送消息的归拢到一起的时候,最下边会有一个消息摘要。默认格式是: n more notifications from xxx 。不过此格式我们是可以定制的。

第一种

?
1
2
3
4
5
6
7
let summaryformat = "%u 更多消息啦啦"
return unnotificationcategory(identifier: "category-identifier",
actions: [],
intentidentifiers: [],
hiddenpreviewsbodyplaceholder: nil,
categorysummaryformat: summaryformat,
options: [])

第二种 let summaryformat = "%u 更多消息啦啦!来自oceanfish"

?
1
2
3
let content = unmutablenotificationcontent()
content.body = "..."
content.summaryargument = "oceanfish"

同一个category的不同格式,苹果会将其合并在一起;并且不同的 summaryargument 苹果也会将其默认合并到一起进行显示

也可以通过 let summaryformat = nsstring.localizedusernotificationstring(forkey: "notification_summary", arguments: nil) 来进行本地化服务

数字定制

有时会出现另一个场景:比如发送了2条推送消息,一条是“你有3个邀请函”,另一条是“你有5个邀请函”。那摘要则会显示你有2更多消息。这显然不是我们想要的!我们最好的期望肯定是"你有8个邀请函"。那这种效果怎么显示呢?

苹果给我们提供了另外一个属性,结合上边的摘要(summary)格式定制我们可以实现以上效果。

?
1
2
3
4
5
let content = unmutablenotificationcontent()
content.body = "..."
content.threadidentifier = "..."
content.summaryargument = "song by song"
content.summaryargumentcount = 3

当多个消息归拢到一起的时候,苹果会将 summaryargumentcount 值加在一起,然后进行显示

推送内容扩展中的可交互和动态更改action

之前消息是不支持交互的和动态更改action的,比如界面有个空心喜欢按钮,用户点击则变成了实心喜欢按钮;有个acction显示“喜欢”,用户点击之后变成"不喜欢"

推送界面可交互

iOS12新特性之推送通知详解

如上图推送界面有个空心喜欢按钮

首先配置notification content extention的 uunnotificationextensionuserinteractionenabled 为 yes

iOS12新特性之推送通知详解

然后代码实现

?
1
2
3
4
5
6
7
8
9
10
11
12
import usernotificationsui
class notificationviewcontroller: uiviewcontroller, unnotificationcontentextension {
 
 @iboutlet var likebutton: uibutton?
 
 likebutton?.addtarget(self, action: #selector(likebuttontapped), for: .touchupinside)
 
 @objc func likebuttontapped() {
  likebutton?.settitle("♥", for: .normal)
  likedphoto()
 }
}

action动态化

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// notification content extensions
class notificationviewcontroller: uiviewcontroller, unnotificationcontentextension {
 
 func didreceive(_ response: unnotificationresponse, completionhandler completion:
  (unnotificationcontentextensionresponseoption) -> void) {
  if response.actionidentifier == "like-action" {
   // update state...
   let unlikeaction = unnotificationaction(identifier: "unlike-action",
             title: "unlike", options: [])
   let currentactions = extensioncontext?.notificationactions
   let commentaction = currentactions![1]
   let newactions = [ unlikeaction, commentaction ]
   extensioncontext?.notificationactions = newactions
  }
 }
}

performnotificationdefaultaction() 用于点击推送的时候启动应用; dismissnotificationcontentextension() 用于关闭锁屏页面的推送具体一条消息

推送消息的管理

这个主要是苹果针对消息增加了一个“管理”的按钮,消息左滑即可出现。

帮助我们快速的针对消息进行设置。

  • deliver quietly 则会不会播放声音。
  • turn off 则会关闭推送
  • setttings 我们可以自己定制
?
1
2
3
4
5
6
7
import uikit
import usernotifications
class appdelegate: uiapplicationdelegate, unusernotificationcenterdelegate {
 func usernotificationcenter(_ center: unusernotificationcenter,
        opensettingsfor notification: unnotification? ) {
 }
}

临时授权

临时授权主要体现就是推送消息过来会有两个按钮,会主动让用户自己选择

?
1
2
3
let notificationcenter = unusernotificationcenter.current()
noficationcenter.requestauthorization(options: [.badge,.alert,.sound,.provisional]) { (tag, error) in
}

在申请权限的时候,加上 provisional 即可。

警告消息

比如家庭安全、健康、公共安全等因素的时候。此消息需要用户必须采取行动。最简单的一个场景是家里安装了一个摄像头,我们去上班了,此时如果家中有人,则摄像头会推送消息给我们。

证书申请 https://developer.apple.com/contact/request/notifications-critical-alerts-entitlement/

本地权限申请

?
1
2
3
let notificationcenter = unusernotificationcenter.current()
noficationcenter.requestauthorization(options: [.badge,.alert,.sound,.criticalalert]) { (tag, error) in
}

在申请权限的时候,加上 criticalalert 。

播放声音

?
1
2
3
4
5
let content = unmutablenotificationcontent()
content.title = "warning: low blood sugar"
content.body = "glucose level at 57."
content.categoryidentifier = "low-glucose—alert"
content.sound = unnotificationsound.criticalsoundnamed(@"warning-sound" withaudiovolume: 1.00)
?
1
2
3
4
5
6
7
8
9
10
11
12
13
// critical alert push payload
{
 // critical alert push payload
 {
  "aps" : {
   "sound" : {
    "critical": 1,
   }
  }
  "name": "warning-sound.aiff",
  "volume": 1.0
 }
}

总结

至此wwdc中关于推送都已经整理完毕。大家有不懂的欢迎留言相互交流

引用

源码using, managing, and customizing notifications

what's new in user notifications

using grouped notifications

好了,以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对服务器之家的支持。

原文链接:http://www.cocoachina.com/ios/20180627/23944.html

延伸 · 阅读

精彩推荐
  • IOSIOS 屏幕适配方案实现缩放window的示例代码

    IOS 屏幕适配方案实现缩放window的示例代码

    这篇文章主要介绍了IOS 屏幕适配方案实现缩放window的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要...

    xiari5772021-06-01
  • IOSiOS通过逆向理解Block的内存模型

    iOS通过逆向理解Block的内存模型

    自从对 iOS 的逆向初窥门径后,我也经常通过它来分析一些比较大的应用,参考一下这些应用中某些功能的实现。这个探索的过程乐趣多多,不仅能满足自...

    Swiftyper12832021-03-03
  • IOS关于iOS自适应cell行高的那些事儿

    关于iOS自适应cell行高的那些事儿

    这篇文章主要给大家介绍了关于iOS自适应cell行高的那些事儿,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的...

    daisy6092021-05-17
  • IOSiOS中tableview 两级cell的展开与收回的示例代码

    iOS中tableview 两级cell的展开与收回的示例代码

    本篇文章主要介绍了iOS中tableview 两级cell的展开与收回的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    J_Kang3862021-04-22
  • IOS解析iOS开发中的FirstResponder第一响应对象

    解析iOS开发中的FirstResponder第一响应对象

    这篇文章主要介绍了解析iOS开发中的FirstResponder第一响应对象,包括View的FirstResponder的释放问题,需要的朋友可以参考下...

    一片枫叶4662020-12-25
  • IOSiOS布局渲染之UIView方法的调用时机详解

    iOS布局渲染之UIView方法的调用时机详解

    在你刚开始开发 iOS 应用时,最难避免或者是调试的就是和布局相关的问题,下面这篇文章主要给大家介绍了关于iOS布局渲染之UIView方法调用时机的相关资料...

    windtersharp7642021-05-04
  • IOSIOS开发之字典转字符串的实例详解

    IOS开发之字典转字符串的实例详解

    这篇文章主要介绍了IOS开发之字典转字符串的实例详解的相关资料,希望通过本文能帮助到大家,让大家掌握这样的方法,需要的朋友可以参考下...

    苦练内功5832021-04-01
  • IOSiOS 雷达效果实例详解

    iOS 雷达效果实例详解

    这篇文章主要介绍了iOS 雷达效果实例详解的相关资料,需要的朋友可以参考下...

    SimpleWorld11022021-01-28