0%

Notification Content Extension

Notification Content Extension

[TOC]

樣式

目前確定內容可放入:

  • 圖片
  • 聲音
  • 影片

測試用資料格式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
{
"Simulator Target Bundle": "com.wondercise.NotificationTest",
"aps" : {
"alert" : {
"title" : "This is title",
"body" : "Near the center of this sharp cosmic portrait, at the heart of the Orion Nebula,
are four hot, massive stars known as the Trapezium. Gathered within a region about 1.5
light-years in radius, they dominate the core of the dense Orion Nebula Star Cluster. Ultraviolet
ionizing radiation from the Trapezium stars, mostly from the brightest star Theta-1 Orionis C
powers the complex star forming region's entire visible glow. About three million years old, the
Orion Nebula Cluster was even more compact in its younger years and a recent dynamical study indicates
that runaway stellar collisions at an earlier age may have formed a black hole with more than 100 times
the mass of the Sun. The presence of a black hole within the cluster could explain the observed high
velocities of the Trapezium stars. The Orion Nebula's distance of some 1,500 light-years would make it
the closest known black hole to planet Earth."
,
"mutable-content" : "1",
"category" : "rexNotificationCategoryIdentifier",
"imageURL": "http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/images/BigBuckBunny.jpg"
}
}

“category” : “rexNotificationCategoryIdentifier”
會影響後續對應的自訂義按鈕製作或事件處理。

Info.plist 設定

UNNotificationExtensionCategory (可依照需求設定多個)

UNNotificationExtensionInitialContentSizeRatio = 1 

UNNotificationExtensionDefaultContentHidden = 1 (隱藏原本推播內容,建議開啟,否則會出現兩段一樣的標題文字)

接收資料

於下列函式可收到推播內容

1
2
3
func didReceive(_ notification: UNNotification) {
//處理資料
}

自定義按鈕

UNNotificationAction可自訂義,利用category identifier做區分。

category的identifier 會與 push notifcation 送來的 category 做對應並處理。

範例:

自訂四種按鈕,創建不同的category後,將UNNotificationAction加入UNNotificationCategory的actions陣列中,在Notification Content Extension的viewDidLoad中初始化。

收到推播後,會依照推播所傳送的category製作對應樣式的按鈕。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
func setCategories() {
//-- Action --
let actionRelay = UNNotificationAction(
identifier: "rexNotificationCategoryIdentifier.action.reply",
title: "Reply",
options: []
)

let actionOK = UNNotificationAction(
identifier: "rexNotificationCategoryIdentifier.action.ok",
title: "OK",
options: []
)

let actionReject = UNNotificationAction(
identifier: "rexNotificationCategoryIdentifier.action.reject",
title: "Reject",
options: []
)

let actionCancel = UNNotificationAction(
identifier: "rexNotificationCategoryIdentifier.action.cancel",
title: "Cancel",
options: []
)

//-- category --

let categoryNormal = UNNotificationCategory(
identifier: rexNotificationCategoryNormalIdentifier,
actions: [actionOK],
intentIdentifiers: [],
options: []
)

let categoryReply = UNNotificationCategory(
identifier: rexNotificationCategoryReplyIdentifier,
actions: [actionRelay, actionOK],
intentIdentifiers: [],
options: []
)

let categoryAll = UNNotificationCategory(
identifier: rexNotificationCategoryAllIdentifier,
actions: [actionRelay, actionOK, actionReject, actionCancel],
intentIdentifiers: [],
options: []
)

UNUserNotificationCenter.current().setNotificationCategories(
[categoryNormal, categoryReply, categoryAll])
}

處理按鈕事件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
func didReceive(_ response: UNNotificationResponse, completionHandler completion: @escaping (UNNotificationContentExtensionResponseOption) -> Void) {
// 點擊按鈕觸發後的動作    
let userInfo = response.notification.request.content.userInfo
if response.notification.request.content.categoryIdentifier == "rexNotificationCategoryIdentifier" {
// Retrieve the meeting details.
switch response.actionIdentifier {
case "rexNotificationCategoryIdentifier.action.reply":
break
case "rexNotificationCategoryIdentifier.action.ok":
break
case UNNotificationDefaultActionIdentifier,
UNNotificationDismissActionIdentifier:
break
default:
break
}
} else {
// Handle other notification types...
}
// Always call the completion handler when done.
completion(.doNotDismiss)
}

輸入訊息

若要輸入訊息,則創建Action時序要使用UNTextInputNotificationAction

1
2
3
4
5
let replyAction = UNTextInputNotificationAction(
identifier: "rexNotificationCategoryIdentifier.action.reply",
title: "Reply",
textInputButtonTitle: "Send",
textInputPlaceholder: "Input text here")

處理回覆

在下列函式中做對應的處理

1
2
3
4
5
6
7
override func didReceive(_ response: UNNotificationResponse, completionHandler completion: @escaping (UNNotificationContentExtensionResponseOption) -> Void) {

if let textResponse = response as? UNTextInputNotificationResponse {
let sendText = textResponse.userText
print("Received text message: \(sendText)")
}
}

AppDelegate

觸發位置

需要 import UserNotifications,實作 UNUserNotificationCenterDelegate

點擊推播的觸發位置

1
2
3
4
5
6
func userNotificationCenter(_: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
print("點擊推播")
let userInfo = response.notification.request.content.userInfo
print("userInfo:\(userInfo)")
completionHandler()
}

開始APP時收到推播的觸發位置

1
2
3
4
5
6
7
8
9
10
11
func userNotificationCenter(_: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
if UIApplication.shared.applicationState != .inactive && UIApplication.shared.applicationState != .background {
print("開始APP時收到推播")
print("WillPresent app")

// App 開啟
let userInfo = notification.request.content.userInfo
print("userInfo:\(userInfo)")
}
completionHandler([])
}
tags: Swift 推播