麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 系統 > iOS > 正文

iOS12新特性之推送通知詳解

2019-10-21 18:40:53
字體:
來源:轉載
供稿:網友

序言

眾所周知,iOS中消息推送扮演了不可或缺的位置。不管是本地通知還是遠程通知無時不刻的在影響著我們的用戶體驗,以致于在iOS10的時候蘋果對推送大規模重構,獨立了已 UserNotifications 和 UserNotificationsUI 兩個單獨的framework,可見重要性一斑。針對于WWDC18蘋果又給我們帶來了什么驚喜呢?

新特性

  • Grouped notifications 推送分組
  • Notification content extensions 推送內容擴展中的可交互和動態更改Action
  • Notification management 推送消息的管理
  • Provisional authorization 臨時授權
  • Critical alerts 警告性質的推送

推送分組

隨著手機上應用的增多,尤其QQ和微信這兩大聊天工具,當手機鎖屏的時候,伴隨著就是好滿屏的推送消息。這一現象不知大家有沒有覺著不高效和體驗性比較差呢?蘋果針對鎖屏情況下,對消息進行了分組,從而有效的提高了用戶的交互體驗,分組形式如下:

iOS12,推送通知

分組形式:

  • 蘋果會自動幫我們以APP的為分類依據進行消息的分組;
  • 如果我們設置了 threadIdentifier 屬性則以此屬性為依據,進行分組。

iOS12,推送通知

代碼如下:

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 。不過此格式我們是可以定制的。

第一種

let summaryFormat = "%u 更多消息啦啦"return UNNotificationCategory(identifier: "category-identifier",actions: [],intentIdentifiers: [],hiddenPreviewsBodyPlaceholder: nil,categorySummaryFormat: summaryFormat,options: [])

第二種 let summaryFormat = "%u 更多消息啦啦!來自OceanFish"

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)格式定制我們可以實現以上效果。

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,推送通知

然后代碼實現

import UserNotificationsUIclass NotificationViewController: UIViewController, UNNotificationContentExtension { @IBOutlet var likeButton: UIButton? likeButton?.addTarget(self, action: #selector(likeButtonTapped), for: .touchUpInside) @objc func likeButtonTapped() {  likeButton?.setTitle("♥", for: .normal)  likedPhoto() }}

Action動態化

// Notification Content Extensionsclass 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 我們可以自己定制
import UIKitimport UserNotificationsclass AppDelegate: UIApplicationDelegate, UNUserNotificationCenterDelegate { func userNotificationCenter(_ center: UNUserNotificationCenter,        openSettingsFor notification: UNNotification? ) { }}

臨時授權

臨時授權主要體現就是推送消息過來會有兩個按鈕,會主動讓用戶自己選擇

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/

本地權限申請

let notificationCenter = UNUserNotificationCenter.current()noficationCenter.requestAuthorization(options: [.badge,.alert,.sound,.criticalAlert]) { (tag, error) in}

在申請權限的時候,加上 criticalAlert 。

播放聲音

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)
// 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

好了,以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對VEVB武林網的支持。


注:相關教程知識閱讀請移步到IOS開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 香蕉久草在线 | av在线日韩 | 精品成人国产在线观看男人呻吟 | 污版视频在线观看 | 亚洲特黄a级毛片在线播放 久久久入口 | 欧美日韩国产成人在线 | 91在线色| 欧美aaaaa一级毛片在线 | 蜜桃91麻豆 | 国产成人免费精品 | 精精国产xxxx视频在线野外 | 一级一级一级一级毛片 | 亚洲四播房| av电影在线观看网址 | 黄色片在线观看网站 | 欧美日韩一区,二区,三区,久久精品 | 可以看逼的视频 | 久久精品国产一区二区 | 中文字幕一区在线观看视频 | 舌头伸进添的我好爽高潮网站 | 亚洲精品日韩色噜噜久久五月 | 日日噜噜噜夜夜狠狠久久蜜桃 | 91小视频在线观看免费版高清 | 麻豆国产网站 | 久久99精品久久久久久236 | 黄色久| 国产精品一区二区x88av | 久久精品99久久久久久2456 | 老a影视网站在线观看免费 国产精品久久久久久久久久尿 | 国产精品刺激对白麻豆99 | 黄色网址电影 | 日韩精品a在线观看 | 91麻豆精品国产91久久久无需广告 | 亚洲午夜在线 | 久草在线网址 | 91精品国产乱码久久久久 | 天天色人人爱 | 凹凸成人精品亚洲精品密奴 | 久久精品日产高清版的功能介绍 | 精品一区二区三区免费毛片爱 | 777午夜精品视频在线播放 |