-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathAppDelegate.swift
More file actions
100 lines (83 loc) · 3.84 KB
/
Copy pathAppDelegate.swift
File metadata and controls
100 lines (83 loc) · 3.84 KB
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
import UIKit
import Flutter
import mindbox_ios
import Mindbox
import UserNotifications
@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
private var eventSink: FlutterEventSink?
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
UNUserNotificationCenter.current().delegate = self
// tracking sources of referrals to the application via push notifications
Mindbox.shared.track(.launch(launchOptions))
// registering background tasks for iOS above 13
if #available(iOS 13.0, *) {
Mindbox.shared.registerBGTasks()
} else {
UIApplication.shared.setMinimumBackgroundFetchInterval(UIApplication.backgroundFetchIntervalMinimum)
}
//Used for notification center
let controller: FlutterViewController = window?.rootViewController as! FlutterViewController
let eventChannel = FlutterEventChannel(name: "cloud.mindbox.flutter_example.notifications", binaryMessenger: controller.binaryMessenger)
eventChannel.setStreamHandler(self)
GeneratedPluginRegistrant.register(with: self)
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
override func application(
_ application: UIApplication,
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
// Transfer to SDK APNs token
Mindbox.shared.apnsTokenUpdate(deviceToken: deviceToken)
}
override func application(
_ application: UIApplication,
continue userActivity: NSUserActivity,
restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void
) -> Bool {
// Passing the link if the application is opened via universalLink
Mindbox.shared.track(.universalLink(userActivity))
return super.application(application, continue: userActivity, restorationHandler:
restorationHandler)
}
// Register background tasks for iOS up to 13
override func application(
_ application: UIApplication,
performFetchWithCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
Mindbox.shared.application(application, performFetchWithCompletionHandler: completionHandler)
}
override func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
//Implement display of standard notifications
completionHandler([.alert, .badge, .sound])
notifyFlutterNewData()
}
override func userNotificationCenter(
_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void
) {
// Send click to Mindbox
Mindbox.shared.pushClicked(response: response)
// Sending the fact that the application was opened when switching to push notification
Mindbox.shared.track(.push(response))
completionHandler()
super.userNotificationCenter(center, didReceive: response, withCompletionHandler: completionHandler)
}
func notifyFlutterNewData() {
if let eventSink = eventSink {
eventSink("newNotification")
}
}
}
extension AppDelegate: FlutterStreamHandler {
func onListen(withArguments arguments: Any?, eventSink events: @escaping FlutterEventSink) -> FlutterError? {
self.eventSink = events
return nil
}
func onCancel(withArguments arguments: Any?) -> FlutterError? {
self.eventSink = nil
return nil
}
}