Proyectos de Subversion Iphone Microlearning - Nuevo Interface

Rev

Rev 8 | Rev 11 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
//
2
//  AppDelegate.swift
3
//  twogetskills
4
//
5
//  Created by Efrain Yanez Recanatini on 2/23/22.
6
//
7
 
8
import Foundation
9
import Firebase
10
import Messages
11
import BackgroundTasks
12
 
13
//UIResponder, UIApplicationDelegate {
14
 
15
class AppDelegate : NSObject, UIApplicationDelegate {
16
    private var isSyncInProgress = false
17
    private var timer = Timer()
18
    private var syncAdapter = SyncAdapter()
19
    private let gcmMessageIDKey = "gcm.message_id"
9 efrain 20
    private let appDao = AppDao.sharedInstance
1 efrain 21
 
22
    static var orientationLock = UIInterfaceOrientationMask.portrait
23
 
24
    func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
25
        return AppDelegate.orientationLock
26
    }
27
 
28
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions:
29
        [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
30
 
31
 
32
 
33
 
34
        print("registro del dispositivo")
35
        NotificationCenter.default.addObserver(self, selector: #selector(receivingNewDevice(n:)), name: Constants.NOTIFICATION_NAME_REGISTER_NEW_DEVICE, object: nil)
36
 
37
        if let uuid = UIDevice.current.identifierForVendor?.uuidString {
38
            let userInfo = ["uuid": uuid]
39
            NotificationCenter.default.post(name: Constants.NOTIFICATION_NAME_REGISTER_NEW_DEVICE , object: self, userInfo: userInfo)
40
        }
41
 
42
 
43
 
44
        NotificationCenter.default.addObserver(self, selector: #selector(receivingNewToken(n:)), name: Constants.NOTIFICATION_NAME_REGISTER_NEW_FCM_TOKEN, object: nil)
45
 
46
        // Remote Notification Enable// Use Firebase library to configure APIs
47
 
48
 
49
        print("Firebase configuracion")
50
        FirebaseApp.configure()
51
 
52
        UNUserNotificationCenter.current().delegate = self
53
        let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
54
        UNUserNotificationCenter.current().requestAuthorization(
55
            options: authOptions,
56
            completionHandler: {_, _ in }
57
        )
58
 
59
 
60
 
61
 
62
        Messaging.messaging().delegate = self
63
 
64
        print("Register For Remote Notifications")
65
        application.registerForRemoteNotifications()
66
 
67
 
68
 
69
 
70
 
71
        return true;
72
 
73
    }
74
 
75
 
8 efrain 76
 
1 efrain 77
    // Storage of the UID once the notification is received
78
    @objc func receivingNewDevice(n: NSNotification){
79
 
80
            print("receivingNewDevice")
81
            if n.userInfo != nil{
82
                if let new_uuid = n.userInfo?["uuid"]! as? String {
83
 
84
                    let old_uuid = appData.deviceUuid
85
                    if new_uuid != old_uuid {
86
                        var sync = SyncModel()
87
                        sync.type = Constants.SYNC_ADAPTER_TYPE_DEVICE
88
                        sync.data = new_uuid
89
 
90
                        if SyncDao.sharedInstance.insert(record: sync) > 0 {
91
                            appData.deviceUuid = new_uuid
92
                            appData.save()
93
 
94
 
95
 
96
                            syncAdapter.sync {
97
                                    success in
98
                            }
99
                        }
100
                    }
101
                }
102
            }
103
        }
104
 
105
        @objc func receivingNewToken(n: NSNotification){
106
            if n.userInfo != nil{
107
                if let token = n.userInfo?["token"]! as? String {
108
                    if !token.isEmpty {
109
 
110
                        if appData.fcmToken != token  {
111
 
112
                            var sync = SyncModel()
113
                            sync.data = token
114
                            sync.type = Constants.SYNC_ADAPTER_TYPE_FCM
115
 
116
                            if SyncDao.sharedInstance.insert(record: sync) > 0 {
117
 
118
                                appData.fcmToken = token
119
                                appData.save()
120
 
121
                                syncAdapter.sync {
122
                                        success in
123
                                }
124
                            }
125
                        }
126
                    }
127
                }
128
            }
129
        }
130
}
131
 
132
//Recibir mensajes
133
@available(iOS 10, *)
134
extension AppDelegate: UNUserNotificationCenterDelegate {
135
  // Receive displayed notifications for iOS 10 devices.
136
  func userNotificationCenter(_ center: UNUserNotificationCenter,
137
                              willPresent notification: UNNotification,
138
                              withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions)
139
                                -> Void) {
140
 
141
    print("message userNotificationCenter iOS 10")
142
 
143
    let userInfo = notification.request.content.userInfo
144
 
145
    // With swizzling disabled you must let Messaging know about the message, for Analytics
146
    // Messaging.messaging().appDidReceiveMessage(userInfo)
147
 
148
    // ...
149
    print(userInfo)
8 efrain 150
 
151
    if let aps = userInfo["aps"] as? [String:Any] {
152
      //let badgeNumber = aps["badge"] as! Int
153
      //application.applicationIconBadgeNumber = badgeNumber
154
 
155
        //print("aps: \(aps)")
1 efrain 156
 
8 efrain 157
 
158
        //if let alert = aps["alert"]as? [String:Any] {
159
 
160
           // let title = alert["title"] ?  alert["title"]!  : ""
161
           // let body = alert["body"] ? alert["body"]! : ""
162
 
163
            //if !title.isEmpty  && !body.isEmpty {
164
 
165
            //    var userNotification = UserNotificationModel()
166
            //    userNotification.title = title
167
            //    userNotification.description = body
168
            //}
169
        //}
170
        //let title = aps["title"] as! String
171
        //let body = aps["body"] as! String
172
 
173
        // Print full message.
174
        //print("userInfo title: \(title)")
175
        //print("userInfo body: \(body)")
176
 
177
        if let new_capsules = userInfo["new_capsules"] as? Int {
178
              print("new_capsules : \(new_capsules)") // output: "some-value"
179
           }
180
    }
181
 
182
    /*
183
     message userNotificationCenter iOS 10
184
     [AnyHashable("aps"): {
185
         alert =     {
186
             body = "Cuerpo notificaci\U00f3n";
187
             subtitle = "Sub t\U00edtulo notificaci\U00f3n";
188
             title = "T\U00edtulo notificaci\U00f3n";
189
         };
190
     }]
191
     aps: ["alert": {
192
         body = "Cuerpo notificaci\U00f3n";
193
         subtitle = "Sub t\U00edtulo notificaci\U00f3n";
194
         title = "T\U00edtulo notificaci\U00f3n";
195
     }]
196
     */
197
 
198
 
199
 
200
 
1 efrain 201
    // Change this to your preferred presentation option
202
    completionHandler([[.alert, .sound]])
203
  }
204
 
205
  func userNotificationCenter(_ center: UNUserNotificationCenter,
206
                              didReceive response: UNNotificationResponse,
207
                              withCompletionHandler completionHandler: @escaping () -> Void) {
208
 
209
 
210
    print("message userNotificationCenter")
211
    let userInfo = response.notification.request.content.userInfo
212
 
213
    // ...
214
 
215
    // With swizzling disabled you must let Messaging know about the message, for Analytics
216
    // Messaging.messaging().appDidReceiveMessage(userInfo)
217
 
218
    // Print full message.
8 efrain 219
    print("userInfo : \(userInfo)")
1 efrain 220
 
221
    completionHandler()
222
  }
223
}
224
 
225
 
226
 
227
 
228
extension AppDelegate: MessagingDelegate {
229
    func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String?) {
230
 
231
        Messaging.messaging().token { token, error in
232
            if let error = error {
233
                print("Error fetching FCM registration token: \(error)")
234
            } else if let token = token {
235
                let userInfo = ["token": token]
236
                NotificationCenter.default.post(name: Constants.NOTIFICATION_NAME_REGISTER_NEW_FCM_TOKEN , object: self, userInfo: userInfo)
237
                print("FCM REGISTRATION TOKEN: \(token)")
238
            }
239
        }
240
    }
241
}
242
 
243
 
244