Skip to content

Commit 933ecee

Browse files
More notification tests.
1 parent 5ff2aee commit 933ecee

File tree

4 files changed

+118
-59
lines changed

4 files changed

+118
-59
lines changed

Tests/ui-tests/UITests.swift

Lines changed: 57 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class UITests: XCTestCase {
4040
// Put teardown code here. This method is called after the invocation of each test method in the class.
4141
}
4242

43-
func testSendNotificationOpenGoogle() {
43+
func testSendNotificationOpenSafari() {
4444
allowNotificationsIfNeeded()
4545

4646
app.buttons["Send Notification"].tap()
@@ -53,7 +53,7 @@ class UITests: XCTestCase {
5353
// Give one second pause before interacting
5454
sleep(1)
5555

56-
let button = SpringBoardNotification(springboard: springboard).buttonOpenGoogle
56+
let button = SpringBoardNotification(springboard: springboard).buttonOpenSafari
5757
button.tap()
5858

5959
// Give some time to open
@@ -66,6 +66,51 @@ class UITests: XCTestCase {
6666
// launch this app again for other tests
6767
app.launch()
6868
}
69+
70+
func testSendNotificationOpenDeeplink() {
71+
allowNotificationsIfNeeded()
72+
73+
app.buttons["Send Notification"].tap()
74+
75+
let notification = springboard.otherElements["NotificationShortLookView"]
76+
XCTAssert(notification.waitForExistence(timeout: 10))
77+
78+
notification.swipeDown()
79+
80+
// Give one second pause before interacting
81+
sleep(1)
82+
83+
let button = SpringBoardNotification(springboard: springboard).buttonOpenDeeplink
84+
button.tap()
85+
86+
// Give some time to open
87+
sleep(1)
88+
89+
waitForElementToAppear(app.staticTexts["https://www.myuniqueurl.com"])
90+
}
91+
92+
func testSendNotificationCustomAction() {
93+
allowNotificationsIfNeeded()
94+
95+
app.buttons["Send Notification"].tap()
96+
97+
let notification = springboard.otherElements["NotificationShortLookView"]
98+
XCTAssert(notification.waitForExistence(timeout: 10))
99+
100+
notification.swipeDown()
101+
102+
// Give one second pause before interacting
103+
sleep(1)
104+
105+
let button = SpringBoardNotification(springboard: springboard).buttonCustomAction
106+
button.tap()
107+
108+
// Give some time to open
109+
sleep(1)
110+
111+
waitForElementToAppear(app.staticTexts["MyUniqueCustomAction"])
112+
}
113+
69114

70115
func testShowSystemNotification() {
71116
// Tap the Left Button
@@ -167,9 +212,16 @@ class UITests: XCTestCase {
167212
struct SpringBoardNotification {
168213
let springboard: XCUIApplication
169214

170-
var buttonOpenGoogle: XCUIElement {
171-
return springboard.buttons["Open Google"].firstMatch
215+
var buttonOpenSafari: XCUIElement {
216+
return springboard.buttons["Open Safari"].firstMatch
217+
}
218+
219+
var buttonOpenDeeplink: XCUIElement {
220+
return springboard.buttons["Open Deeplink"].firstMatch
172221
}
173-
}
174222

223+
var buttonCustomAction: XCUIElement {
224+
return springboard.buttons["Custom Action"].firstMatch
225+
}
226+
}
175227

host-app/AppDelegate.swift

Lines changed: 7 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
1717

1818
var window: UIWindow?
1919

20-
2120
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
2221
// Override point for customization after application launch.
2322

@@ -51,42 +50,12 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
5150
func applicationWillTerminate(_ application: UIApplication) {
5251
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
5352
}
54-
55-
@available(iOS 10.0, *)
56-
private func setupNotifications() {
57-
UNUserNotificationCenter.current().delegate = self
58-
UNUserNotificationCenter.current().getNotificationSettings { (settings) in
59-
if settings.authorizationStatus != .authorized {
60-
print("Not authorized")
61-
// not authorized, ask for permission
62-
UNUserNotificationCenter.current().requestAuthorization(options:[.alert, .badge, .sound]) { (success, error) in
63-
print("authGranted: \(success)")
64-
if success {
65-
66-
}
67-
}
68-
} else {
69-
// already authorized
70-
}
71-
}
72-
}
73-
}
74-
75-
@available(iOS 10.0, *)
76-
extension AppDelegate : UNUserNotificationCenterDelegate {
77-
public func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
78-
completionHandler([.alert, .badge, .sound])
79-
}
80-
81-
// The method will be called on the delegate when the user responded to the notification by opening the application, dismissing the notification or choosing a UNNotificationAction. The delegate must be set before the application returns from applicationDidFinishLaunching:.
82-
public func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
83-
IterableAppIntegration.userNotificationCenter(center, didReceive: response, withCompletionHandler: completionHandler)
84-
}
8553
}
8654

8755
extension AppDelegate : IterableCustomActionDelegate {
8856
func handle(iterableCustomAction action: IterableAction, inContext context: IterableActionContext) -> Bool {
8957
ITBInfo("handleCustomAction: \(action)")
58+
NotificationCenter.default.post(name: .handleIterableCustomAction, object: nil, userInfo: ["name" : action.type])
9059
return true
9160
}
9261
}
@@ -99,7 +68,13 @@ extension AppDelegate : IterableURLDelegate {
9968
return false
10069
} else {
10170
// I am handling this
71+
NotificationCenter.default.post(name: .handleIterableUrl, object: nil, userInfo: ["url" : url.absoluteString])
10272
return true
10373
}
10474
}
10575
}
76+
77+
extension Notification.Name {
78+
static let handleIterableUrl = Notification.Name("handleIterableUrl")
79+
static let handleIterableCustomAction = Notification.Name("handleIterableCustomAction")
80+
}

host-app/ViewController.swift

Lines changed: 54 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ class ViewController: UIViewController {
1616

1717
override func viewDidLoad() {
1818
super.viewDidLoad()
19+
20+
NotificationCenter.default.addObserver(self, selector: #selector(handleNotification(_:)), name: .handleIterableUrl, object: nil)
21+
NotificationCenter.default.addObserver(self, selector: #selector(handleNotification(_:)), name: .handleIterableCustomAction, object: nil)
1922
}
2023

2124
override func didReceiveMemoryWarning() {
@@ -175,20 +178,39 @@ class ViewController: UIViewController {
175178
content.badge = NSNumber(value: 1)
176179

177180
let messageId = UUID().uuidString
181+
let uniqueUrl = "https://www.myuniqueurl.com"
182+
let customActionName = "MyUniqueCustomAction"
178183

179184
let userInfo = [
180185
"itbl": [
181186
"campaignId" : 1234,
182187
"templateId" : 4321,
183188
"isGhostPush" : false,
184189
"messageId" : messageId,
185-
"actionButtons" : [[
186-
"identifier" : "Open Google",
187-
"buttonType" : "default",
188-
"action" : [
189-
"type" : "openUrl",
190-
"data" : "https://www.google.com"
191-
]]
190+
"actionButtons" : [
191+
[
192+
"identifier" : "Open Safari",
193+
"buttonType" : "default",
194+
"action" : [
195+
"type" : "openUrl",
196+
"data" : "https://www.google.com"
197+
],
198+
],
199+
[
200+
"identifier" : "Open Deeplink",
201+
"buttonType" : "default",
202+
"action" : [
203+
"type" : "openUrl",
204+
"data" : uniqueUrl,
205+
],
206+
],
207+
[
208+
"identifier" : "Custom Action",
209+
"buttonType" : "default",
210+
"action" : [
211+
"type" : customActionName,
212+
],
213+
],
192214
]
193215
]
194216
]
@@ -203,14 +225,36 @@ class ViewController: UIViewController {
203225
@available(iOS 10.0, *)
204226
private func registerCategories() {
205227
ITBInfo()
206-
let tapButton1Action = UNNotificationAction(identifier: "Open Google", title: "Open Google", options: .foreground)
207-
let tapButton2Action = UNNotificationAction(identifier: "Button2", title: "Tap Button 2", options: .destructive)
228+
let tapButton1Action = UNNotificationAction(identifier: "Open Safari", title: "Open Safari", options: .foreground)
229+
let tapButton2Action = UNNotificationAction(identifier: "Open Deeplink", title: "Open Deeplink", options: .foreground)
230+
let tapButton3Action = UNNotificationAction(identifier: "Custom Action", title: "Custom Action", options: .foreground)
208231

209-
let category = UNNotificationCategory(identifier: "addButtonsCategory", actions: [tapButton1Action, tapButton2Action], intentIdentifiers: [])
232+
let category = UNNotificationCategory(identifier: "addButtonsCategory", actions: [tapButton1Action, tapButton2Action, tapButton3Action], intentIdentifiers: [])
210233

211234
UNUserNotificationCenter.current().setNotificationCategories([category])
212235
}
213236

237+
@objc private func handleNotification(_ notification: NSNotification) {
238+
ITBInfo()
239+
switch notification.name {
240+
case .handleIterableUrl:
241+
if let userInfo = notification.userInfo {
242+
if let url = userInfo["url"] as? String {
243+
statusLbl.text = url
244+
}
245+
}
246+
break
247+
case .handleIterableCustomAction:
248+
if let userInfo = notification.userInfo {
249+
if let customActionName = userInfo["name"] as? String {
250+
statusLbl.text = customActionName
251+
}
252+
}
253+
break
254+
default:
255+
break
256+
}
257+
}
214258
}
215259

216260
@available(iOS 10.0, *)

swift-sdk/IterableAction.swift

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,6 @@ import Foundation
3232
/** The text response typed by the user */
3333
@objc public var userInput: String?
3434

35-
/**
36-
* Checks whether this action is of a specific type.
37-
* - parmeter type: Action type to match against
38-
* - returns: Bool indicating whether the action type matches the one passed to this method
39-
*/
40-
@objc public func isOfType(_ type: String) -> Bool {
41-
return self.type == type
42-
}
43-
44-
/**
45-
Just a shortcut for `isOfType(IterableAction.actionTypeOpenUrl)`.
46-
*/
4735
@objc public func isOpenUrl() -> Bool {
4836
return self.type == IterableAction.actionTypeOpenUrl
4937
}

0 commit comments

Comments
 (0)