/
ManagementViewController.swift
131 lines (109 loc) · 5.38 KB
/
ManagementViewController.swift
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
//
// ManagementViewController.swift
// UserNotificationDemo
//
// Created by WANG WEI on 2016/08/03.
// Copyright © 2016年 OneV's Den. All rights reserved.
//
import UIKit
import UserNotifications
class ManagementViewController: UIViewController {
let title1Content: UNNotificationContent = {
let content = UNMutableNotificationContent()
content.title = "1"
content.body = "Notification 1"
return content
}()
let title2Content: UNNotificationContent = {
let content = UNMutableNotificationContent()
content.title = "2"
content.body = "Notification 2"
return content
}()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
@IBAction func pendingRemovalPressed(_ sender: AnyObject) {
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 3, repeats: false)
let identifier = UserNotificationType.pendingRemoval.rawValue
let request = UNNotificationRequest(identifier: identifier, content: title1Content, trigger: trigger)
UNUserNotificationCenter.current().add(request) { error in
if let error = error {
UIAlertController.showConfirmAlert(message: error.localizedDescription, in: self)
} else {
print("Notification request added: \(identifier)")
}
}
delay(2) {
print("Notification request removed: \(identifier)")
UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: [identifier])
}
}
@IBAction func pendingUpdatePressed(_ sender: AnyObject) {
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 3, repeats: false)
let identifier = UserNotificationType.pendingUpdate.rawValue
let request = UNNotificationRequest(identifier: identifier, content: title1Content, trigger: trigger)
UNUserNotificationCenter.current().add(request) { error in
if let error = error {
UIAlertController.showConfirmAlert(message: error.localizedDescription, in: self)
} else {
print("Notification request added: \(identifier) with title1")
}
}
delay(2) {
let newTrigger = UNTimeIntervalNotificationTrigger(timeInterval: 1, repeats: false)
// Add new request with the same identifier to update a notification.
let newRequest = UNNotificationRequest(identifier: identifier, content: self.title2Content, trigger: newTrigger)
UNUserNotificationCenter.current().add(newRequest) { error in
if let error = error {
UIAlertController.showConfirmAlert(message: error.localizedDescription, in: self)
} else {
print("Notification request updated: \(identifier) with title2")
}
}
}
}
@IBAction func deliveredRemovalPressed(_ sender: AnyObject) {
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 3, repeats: false)
let identifier = UserNotificationType.deliveredRemoval.rawValue
let request = UNNotificationRequest(identifier: identifier, content: title1Content, trigger: trigger)
UNUserNotificationCenter.current().add(request) { error in
if let error = error {
UIAlertController.showConfirmAlert(message: error.localizedDescription, in: self)
} else {
print("Notification request added: \(identifier)")
}
}
delay(4) {
print("Notification request removed: \(identifier)")
UNUserNotificationCenter.current().removeDeliveredNotifications(withIdentifiers: [identifier])
}
}
// This is for local notification. To update a remote notification,
// you need to set the identifier as the value of `apns-collapse-id` key in an HTTP/2 request header.
@IBAction func deliveredUpdatePressed(_ sender: AnyObject) {
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 3, repeats: false)
let identifier = UserNotificationType.pendingUpdate.rawValue
let request = UNNotificationRequest(identifier: identifier, content: title1Content, trigger: trigger)
UNUserNotificationCenter.current().add(request) { error in
if let error = error {
UIAlertController.showConfirmAlert(message: error.localizedDescription, in: self)
} else {
print("Notification request added: \(identifier) with title1")
}
}
delay(4) {
let newTrigger = UNTimeIntervalNotificationTrigger(timeInterval: 1, repeats: false)
// Add new request with the same identifier to update a notification.
let newRequest = UNNotificationRequest(identifier: identifier, content: self.title2Content, trigger: newTrigger)
UNUserNotificationCenter.current().add(newRequest) { error in
if let error = error {
UIAlertController.showConfirmAlert(message: error.localizedDescription, in: self)
} else {
print("Notification request updated: \(identifier) with title2")
}
}
}
}
}