@@ -5,40 +5,101 @@ import Mindbox
5
5
import UserNotifications
6
6
7
7
@UIApplicationMain
8
- @objc class AppDelegate : MindboxFlutterAppDelegate {
8
+ @objc class AppDelegate : FlutterAppDelegate {
9
9
private var eventSink : FlutterEventSink ?
10
10
11
- override func shouldRegisterForRemoteNotifications( ) -> Bool {
12
- return true
13
- }
14
-
15
11
override func application(
16
12
_ application: UIApplication ,
17
13
didFinishLaunchingWithOptions launchOptions: [ UIApplication . LaunchOptionsKey : Any ] ?
18
14
) -> Bool {
15
+
19
16
UIApplication . shared. registerForRemoteNotifications ( )
20
- GeneratedPluginRegistrant . register ( with: self )
21
17
18
+ // Calling the notification request method
19
+ registerForRemoteNotifications ( )
20
+
21
+ // tracking sources of referrals to the application via push notifications
22
+ Mindbox . shared. track ( . launch( launchOptions) )
23
+
24
+ // registering background tasks for iOS above 13
25
+ if #available( iOS 13 . 0 , * ) {
26
+ Mindbox . shared. registerBGTasks ( )
27
+ } else {
28
+ UIApplication . shared. setMinimumBackgroundFetchInterval ( UIApplication . backgroundFetchIntervalMinimum)
29
+ }
30
+
31
+ //Used for notification center
22
32
let controller : FlutterViewController = window? . rootViewController as! FlutterViewController
23
33
let eventChannel = FlutterEventChannel ( name: " cloud.mindbox.flutter_example.notifications " , binaryMessenger: controller. binaryMessenger)
24
34
eventChannel. setStreamHandler ( self )
25
35
26
- UNUserNotificationCenter . current ( ) . delegate = self
27
-
36
+ GeneratedPluginRegistrant . register ( with: self )
28
37
return super. application ( application, didFinishLaunchingWithOptions: launchOptions)
29
38
}
30
39
31
- func notifyFlutterNewData( ) {
32
- if let eventSink = eventSink {
33
- eventSink ( " newNotification " )
40
+ override func application(
41
+ _ application: UIApplication ,
42
+ didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data ) {
43
+ // Transfer to SDK APNs token
44
+ Mindbox . shared. apnsTokenUpdate ( deviceToken: deviceToken)
34
45
}
35
- }
46
+
47
+ override func application(
48
+ _ application: UIApplication ,
49
+ continue userActivity: NSUserActivity ,
50
+ restorationHandler: @escaping ( [ UIUserActivityRestoring ] ? ) -> Void
51
+ ) -> Bool {
52
+ // Passing the link if the application is opened via universalLink
53
+ Mindbox . shared. track ( . universalLink( userActivity) )
54
+ return super. application ( application, continue: userActivity, restorationHandler:
55
+ restorationHandler)
56
+ }
57
+
58
+ // Register background tasks for iOS up to 13
59
+ override func application(
60
+ _ application: UIApplication ,
61
+ performFetchWithCompletionHandler completionHandler: @escaping ( UIBackgroundFetchResult ) -> Void ) {
62
+ Mindbox . shared. application ( application, performFetchWithCompletionHandler: completionHandler)
63
+ }
64
+
36
65
37
66
override func userNotificationCenter( _ center: UNUserNotificationCenter , willPresent notification: UNNotification , withCompletionHandler completionHandler: @escaping ( UNNotificationPresentationOptions ) -> Void ) {
38
- super. userNotificationCenter ( center, willPresent: notification, withCompletionHandler: completionHandler)
67
+ //Implement display of standard notifications
68
+ completionHandler ( [ . alert, . badge, . sound] )
39
69
notifyFlutterNewData ( )
40
70
}
41
71
72
+ override func userNotificationCenter(
73
+ _ center: UNUserNotificationCenter ,
74
+ didReceive response: UNNotificationResponse ,
75
+ withCompletionHandler completionHandler: @escaping ( ) -> Void ) {
76
+ // Send click to Mindbox
77
+ Mindbox . shared. pushClicked ( response: response)
78
+
79
+ // Sending the fact that the application was opened when switching to push notification
80
+ Mindbox . shared. track ( . push( response) )
81
+ completionHandler ( )
82
+ super. userNotificationCenter ( center, didReceive: response, withCompletionHandler: completionHandler)
83
+ }
84
+
85
+ func registerForRemoteNotifications( ) {
86
+ UNUserNotificationCenter . current ( ) . delegate = self
87
+ DispatchQueue . main. async {
88
+ UNUserNotificationCenter . current ( ) . requestAuthorization ( options: [ . alert, . sound, . badge] ) { granted, error in
89
+ print ( " Permission granted: \( granted) " )
90
+ if let error = error {
91
+ print ( " NotificationsRequestAuthorization failed with error: \( error. localizedDescription) " )
92
+ }
93
+ Mindbox . shared. notificationsRequestAuthorization ( granted: granted)
94
+ }
95
+ }
96
+ }
97
+
98
+ func notifyFlutterNewData( ) {
99
+ if let eventSink = eventSink {
100
+ eventSink ( " newNotification " )
101
+ }
102
+ }
42
103
}
43
104
44
105
extension AppDelegate : FlutterStreamHandler {
0 commit comments