最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

iOS12新特性之推送通知詳解

 更新時(shí)間:2018年06月28日 09:10:26   作者:FlyOceanFish  
這篇文章主要給大家介紹了關(guān)于iOS12新特性之推送通知的相關(guān)資料文中通過圖文以及示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

序言

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

新特性

  • Grouped notifications 推送分組
  • Notification content extensions 推送內(nèi)容擴(kuò)展中的可交互和動(dòng)態(tài)更改Action
  • Notification management 推送消息的管理
  • Provisional authorization 臨時(shí)授權(quán)
  • Critical alerts 警告性質(zhì)的推送

推送分組

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


分組形式:

  • 蘋果會(huì)自動(dòng)幫我們以APP的為分類依據(jù)進(jìn)行消息的分組;
  • 如果我們?cè)O(shè)置了 threadIdentifier 屬性則以此屬性為依據(jù),進(jìn)行分組。


代碼如下:

let content = UNMutableNotificationContent() 
content.title = "Notifications Team" 
content.body = "WWDC session after party" 
content.threadIdentifier = "notifications-team-chat"http://通過這個(gè)屬性設(shè)置分組,如果此屬性沒有設(shè)置則以APP為分組依據(jù)

摘要(Summary)格式定制

當(dāng)蘋果自動(dòng)將推送消息的歸攏到一起的時(shí)候,最下邊會(huì)有一個(gè)消息摘要。默認(rèn)格式是: n more notifications from xxx 。不過此格式我們是可以定制的。

第一種

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

第二種 let summaryFormat = "%u 更多消息啦啦!來自O(shè)ceanFish"

let content = UNMutableNotificationContent() 
content.body = "..." 
content.summaryArgument = "OceanFish"

同一個(gè)category的不同格式,蘋果會(huì)將其合并在一起;并且不同的 summaryArgument 蘋果也會(huì)將其默認(rèn)合并到一起進(jìn)行顯示

也可以通過 let summaryFormat = NSString.localizedUserNotificationString(forKey: "NOTIFICATION_SUMMARY", arguments: nil) 來進(jìn)行本地化服務(wù)

數(shù)字定制

有時(shí)會(huì)出現(xiàn)另一個(gè)場(chǎng)景:比如發(fā)送了2條推送消息,一條是“你有3個(gè)邀請(qǐng)函”,另一條是“你有5個(gè)邀請(qǐng)函”。那摘要?jiǎng)t會(huì)顯示你有2更多消息。這顯然不是我們想要的!我們最好的期望肯定是"你有8個(gè)邀請(qǐng)函"。那這種效果怎么顯示呢?

蘋果給我們提供了另外一個(gè)屬性,結(jié)合上邊的摘要(Summary)格式定制我們可以實(shí)現(xiàn)以上效果。

let content = UNMutableNotificationContent() 
content.body = "..." 
content.threadIdentifier = "..." 
content.summaryArgument = "Song by Song" 
content.summaryArgumentCount = 3

當(dāng)多個(gè)消息歸攏到一起的時(shí)候,蘋果會(huì)將 summaryArgumentCount 值加在一起,然后進(jìn)行顯示

推送內(nèi)容擴(kuò)展中的可交互和動(dòng)態(tài)更改Action

之前消息是不支持交互的和動(dòng)態(tài)更改Action的,比如界面有個(gè)空心喜歡按鈕,用戶點(diǎn)擊則變成了實(shí)心喜歡按鈕;有個(gè)Acction顯示“喜歡”,用戶點(diǎn)擊之后變成"不喜歡"

推送界面可交互

如上圖推送界面有個(gè)空心喜歡按鈕

首先配置Notification Content Extention的 UUNNotificationExtensionUserInteractionEnabled 為 YES

然后代碼實(shí)現(xiàn)

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動(dòng)態(tài)化

// 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() 用于點(diǎn)擊推送的時(shí)候啟動(dòng)應(yīng)用; dismissNotificationContentExtension() 用于關(guān)閉鎖屏頁面的推送具體一條消息

推送消息的管理

這個(gè)主要是蘋果針對(duì)消息增加了一個(gè)“管理”的按鈕,消息左滑即可出現(xiàn)。

幫助我們快速的針對(duì)消息進(jìn)行設(shè)置。

  • Deliver Quietly 則會(huì)不會(huì)播放聲音。
  • turn off 則會(huì)關(guān)閉推送
  • Setttings 我們可以自己定制
import UIKit
import UserNotifications
class AppDelegate: UIApplicationDelegate, UNUserNotificationCenterDelegate {
 func userNotificationCenter(_ center: UNUserNotificationCenter,
        openSettingsFor notification: UNNotification? ) {
 }
}

臨時(shí)授權(quán)

臨時(shí)授權(quán)主要體現(xiàn)就是推送消息過來會(huì)有兩個(gè)按鈕,會(huì)主動(dòng)讓用戶自己選擇

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

在申請(qǐng)權(quán)限的時(shí)候,加上 provisional 即可。

警告消息

比如家庭安全、健康、公共安全等因素的時(shí)候。此消息需要用戶必須采取行動(dòng)。最簡單的一個(gè)場(chǎng)景是家里安裝了一個(gè)攝像頭,我們?nèi)ド习嗔?,此時(shí)如果家中有人,則攝像頭會(huì)推送消息給我們。

證書申請(qǐng) https://developer.apple.com/contact/request/notifications-critical-alerts-entitlement/

本地權(quán)限申請(qǐng)

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

在申請(qǐng)權(quán)限的時(shí)候,加上 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
 }
}

總結(jié)

至此WWDC中關(guān)于推送都已經(jīng)整理完畢。大家有不懂的歡迎留言相互交流

引用

源碼Using, Managing, and Customizing Notifications (本地下載

Using Grouped Notifications

好了,以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對(duì)腳本之家的支持。

相關(guān)文章

最新評(píng)論

房产| 山西省| 汶川县| 霞浦县| 鸡西市| 黄平县| 鱼台县| 哈密市| 富阳市| 新沂市| 旌德县| 惠州市| 遂宁市| 松潘县| 新津县| 仁怀市| 永兴县| 武威市| 积石山| 定日县| 京山县| 昌图县| 文山县| 九台市| 神木县| 永昌县| 吉林市| 衡山县| 新河县| 锦屏县| 界首市| 县级市| 乳山市| 静乐县| 临泉县| 鹰潭市| 娄底市| 邮箱| 平乡县| 综艺| 凉城县|