-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPushManager.swift
52 lines (37 loc) · 1.78 KB
/
PushManager.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
import Foundation
import UIKit
import UserNotifications
import Batch.Push
private let systemPopupTriggeredKey = "systemPopupTriggered"
let singleArticleCategory = "article"
let saveActionIdentifier = "save"
let saveActionLabel = "Save for later"
let addCartActionIdentifier = "add_to_cart"
let addCartActionLabel = "Add to cart"
// Manages push notification registration status and options
class PushManager {
// Track the system notification popup
// Note that this isn't entirely accurate since we can't know if we already did it,
// but we can only know if we asked it once for this install
// Since iOS 10, there is a way to know this for sure. This code doesn't use it
// for now, since it is asynchronous and changes the application flow significantly
var didTriggerSystemPopup: Bool {
get {
return UserDefaults.standard.bool(forKey: systemPopupTriggeredKey)
}
set {
UserDefaults.standard.set(newValue, forKey: systemPopupTriggeredKey)
}
}
func register() {
UNUserNotificationCenter.current().setNotificationCategories(createActions())
BatchPush.requestNotificationAuthorization()
didTriggerSystemPopup = true
}
func createActions() -> Set<UNNotificationCategory> {
let saveAction = UNNotificationAction(identifier: saveActionIdentifier, title: saveActionLabel, options: [])
let addToCartAction = UNNotificationAction(identifier: addCartActionIdentifier, title: addCartActionLabel, options: [.foreground])
let category = UNNotificationCategory(identifier: singleArticleCategory, actions: [saveAction, addToCartAction], intentIdentifiers: [], options: [])
return [category]
}
}