Proyectos de Subversion Iphone Microlearning - Inconcert

Rev

Rev 12 | Rev 17 | Ir a la última revisión | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 12 Rev 15
Línea 11... Línea 11...
11
import BackgroundTasks
11
import BackgroundTasks
Línea 12... Línea 12...
12
 
12
 
Línea 13... Línea 13...
13
//UIResponder, UIApplicationDelegate {
13
//UIResponder, UIApplicationDelegate {
14
 
-
 
15
class AppDelegate : NSObject, UIApplicationDelegate {
14
 
16
    private var isSyncInProgress = false
15
class AppDelegate : NSObject, UIApplicationDelegate {
17
    private var syncAdapter = SyncAdapter()
16
    private let database = Database.sharedInstance
18
    private let gcmMessageIDKey = "gcm.message_id"
-
 
19
    private var appData = AppData.sharedInstance
-
 
-
 
17
    private let gcmMessageIDKey = "gcm.message_id"
-
 
18
    private var appData = AppData.sharedInstance
20
 
19
    private var syncAdapter = SyncAdapter()
Línea 21... Línea 20...
21
 
20
    private var isSyncInProgress = false
22
    static var orientationLock = UIInterfaceOrientationMask.portrait
21
    private static var orientationLock = UIInterfaceOrientationMask.portrait
23
 
22
 
Línea 24... Línea 23...
24
    func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
23
    func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
25
        return AppDelegate.orientationLock
24
        return AppDelegate.orientationLock
Línea 26... Línea -...
26
    }
-
 
27
    
-
 
28
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions:
-
 
29
        [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
-
 
Línea 30... Línea -...
30
 
-
 
31
        let database = Database.sharedInstance
-
 
32
        if database.open() != nil {
-
 
33
              database.createTables()
25
    }
34
        }
26
    
35
        
-
 
36
        if let new_uuid = UIDevice.current.identifierForVendor?.uuidString {
27
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions:
37
           
28
        [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
38
            let old_uuid = appData.deviceUuid
29
 
39
            if new_uuid != old_uuid {
30
        
40
                var sync = SyncModel()
-
 
41
                sync.type = Constants.SYNC_ADAPTER_TYPE_DEVICE
31
        database.open()
42
                sync.data = new_uuid
32
        database.createTables()
43
                
33
        database.close()
44
                if SyncDao.sharedInstance.insert(record: sync) > 0 {
-
 
45
                    appData.deviceUuid = new_uuid
-
 
46
                    appData.save()
-
 
Línea -... Línea 34...
-
 
34
        
-
 
35
        print("observador del UUID del dispositivo")
-
 
36
        NotificationCenter.default.addObserver(self, selector: #selector(receivingNewDevice(n:)), name: .receivingNewDevice, object: nil)
-
 
37
 
-
 
38
        print("observador del FCM TOKEN")
-
 
39
        NotificationCenter.default.addObserver(self, selector: #selector(receivingNewToken(n:)), name: .receivingNewToken, object: nil)
47
                    
40
 
Línea 48... Línea 41...
48
                    syncAdapter.sync {
41
        
Línea 49... Línea 42...
49
                            success in
42
        
Línea 50... Línea 43...
50
                    }
43
        
51
                }
44
        if let new_uuid = UIDevice.current.identifierForVendor?.uuidString {
52
            }
45
            let userInfo = ["uuid": new_uuid]
53
 
46
            NotificationCenter.default.post(name: .receivingNewDevice , object: self, userInfo: userInfo)
54
        }
47
        }
55
              
48
              
56
  
49
  
57
        FirebaseApp.configure()
50
        FirebaseApp.configure()
58
 
51
 
59
                Messaging.messaging().delegate = self
52
        Messaging.messaging().delegate = self
60
 
53
 
61
                if #available(iOS 10.0, *) {
54
        if #available(iOS 10.0, *) {
62
                  // For iOS 10 display notification (sent via APNS)
55
            // For iOS 10 display notification (sent via APNS)
Línea 63... Línea 56...
63
                  UNUserNotificationCenter.current().delegate = self
56
            UNUserNotificationCenter.current().delegate = self
Línea 64... Línea 57...
64
 
57
 
65
                  let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
58
            let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
66
                  UNUserNotificationCenter.current().requestAuthorization(
59
            UNUserNotificationCenter.current().requestAuthorization(
Línea 67... Línea 60...
67
                    options: authOptions,
60
                options: authOptions,
68
                    completionHandler: {_, _ in })
61
                completionHandler: {_, _ in })
Línea -... Línea 62...
-
 
62
        } else {
-
 
63
            let settings: UIUserNotificationSettings =
-
 
64
                UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
-
 
65
            application.registerUserNotificationSettings(settings)
-
 
66
        }
-
 
67
 
-
 
68
        application.registerForRemoteNotifications()
-
 
69
        
-
 
70
        
-
 
71
        
-
 
72
        registerBGTasksScheduler()
-
 
73
        scheduleProcess()
-
 
74
        scheduleRefresh()
-
 
75
        
-
 
76
        return true
-
 
77
    }
-
 
78
    
-
 
79
    // Storage of the UID once the notification is received
-
 
80
       @objc func receivingNewDevice(n: NSNotification){
-
 
81
 
-
 
82
            print("receivingNewDevice")
-
 
83
            if n.userInfo != nil{
-
 
84
                if let new_uuid = n.userInfo?["uuid"]! as? String {
-
 
85
                    let old_uuid = appData.deviceUuid
-
 
86
                    if new_uuid != old_uuid {
-
 
87
                        
-
 
88
                        db = database.open()
-
 
89
                        if db != nil {
-
 
90
                            var sync = SyncModel()
-
 
91
                            sync.type = Constants.SYNC_ADAPTER_TYPE_DEVICE
-
 
92
                            sync.data = new_uuid
-
 
93
                            
-
 
94
                            let syncDao = SyncDao(db : db)
-
 
95
                            if syncDao.insert(record: sync) > 0 {
-
 
96
                                appData.deviceUuid = new_uuid
-
 
97
                                appData.save()
-
 
98
                                
-
 
99
                                syncAdapter.sync {
-
 
100
                                        success in
-
 
101
                                }
-
 
102
                            }
-
 
103
                        }
-
 
104
                        database.close()
-
 
105
                        
-
 
106
                        
-
 
107
                    }
-
 
108
                }
-
 
109
            }
-
 
110
        }
-
 
111
           
-
 
112
        @objc func receivingNewToken(n: NSNotification){
-
 
113
            if n.userInfo != nil{
-
 
114
                if let token = n.userInfo?["token"]! as? String {
-
 
115
                    if !token.isEmpty {
-
 
116
                        if appData.fcmToken != token  {
-
 
117
                            
-
 
118
                            var db = database.open()
-
 
119
                            if db != nil {
-
 
120
                                var sync = SyncModel()
-
 
121
                                sync.data = token
-
 
122
                                sync.type = Constants.SYNC_ADAPTER_TYPE_FCM
-
 
123
                                
-
 
124
                                let syncDao = SyncDao(db: db)
-
 
125
                                
-
 
126
                                if syncDao.insert(record: sync) > 0 {
-
 
127
                                
-
 
128
                                    appData.fcmToken = token
-
 
129
                                    appData.save()
69
                } else {
130
                                    
70
                  let settings: UIUserNotificationSettings =
131
                                    syncAdapter.sync {
Línea 71... Línea 132...
71
                  UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
132
                                            success in
72
                  application.registerUserNotificationSettings(settings)
133
                                    }
Línea 134... Línea 195...
134
        
195
        
135
    func handleProcessTask(task: BGProcessingTask) {
196
    func handleProcessTask(task: BGProcessingTask) {
Línea 136... Línea 197...
136
        scheduleProcess()
197
        scheduleProcess()
137
        
198
        
138
        let syncAdapter = SyncAdapter()
199
        let syncAdapter = SyncAdapter()
139
        syncAdapter.sync { success in
200
        syncAdapter.sync(isForeground: false) { success in
Línea 140... Línea 201...
140
            task.setTaskCompleted(success: success)
201
            task.setTaskCompleted(success: success)
141
        }
202
        }
Línea 169... Línea 230...
169
 
230
 
Línea 170... Línea 231...
170
extension AppDelegate: MessagingDelegate {
231
extension AppDelegate: MessagingDelegate {
Línea -... Línea 232...
-
 
232
    
171
    
233
    func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String?) {
-
 
234
 
-
 
235
       
172
    func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String?) {
236
        let token = fcmToken ?? ""
-
 
237
        print( "MessagingDelegate didReceiveRegistrationToken: \(token)")
Línea 173... Línea -...
173
 
-
 
174
        let token = fcmToken ?? ""
238
        
175
        print("token: ", token)
-
 
176
        
-
 
177
 
-
 
178
        if appData.fcmToken != token  {
-
 
179
            
-
 
180
            var sync = SyncModel()
-
 
181
            sync.data = token
-
 
182
            sync.type = Constants.SYNC_ADAPTER_TYPE_FCM
-
 
183
            
-
 
184
            if SyncDao.sharedInstance.insert(record: sync) > 0 {
-
 
185
            
-
 
186
                appData.fcmToken = token
-
 
187
                appData.save()
-
 
188
                
-
 
189
                syncAdapter.sync {
239
        let userInfo = ["token": token]
190
                        success in
240
           NotificationCenter.default.post(name: .receivingNewToken , object: self, userInfo: userInfo)
Línea 191... Línea 241...
191
                }
241
        
192
            }
242
 
Línea 257... Línea 307...
257
        }
307
        }
258
    }
308
    }
Línea 259... Línea 309...
259
 
309
 
Línea 260... Línea 310...
260
    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
310
    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
Línea 261... Línea 311...
261
 
311
 
262
            print( "didRegisterForRemoteNotificationsWithDeviceToken")
312
        print( "didRegisterForRemoteNotificationsWithDeviceToken")
263
        
313
        
-
 
314
        let tokenParts = deviceToken.map { data in String(format: "%02.2hhx", data) }
264
            let tokenParts = deviceToken.map { data in String(format: "%02.2hhx", data) }
315
        let token = tokenParts.joined()
-
 
316
            print("token: ", token)
-
 
317
        
Línea 265... Línea -...
265
            let token = tokenParts.joined()
-
 
266
            print("token: ", token)
-
 
267
           
-
 
268
 
-
 
269
           if appData.fcmToken != token  {
-
 
270
               
-
 
271
               var sync = SyncModel()
-
 
272
               sync.data = token
-
 
273
               sync.type = Constants.SYNC_ADAPTER_TYPE_FCM
-
 
274
               
-
 
275
               if SyncDao.sharedInstance.insert(record: sync) > 0 {
-
 
276
               
-
 
277
                   appData.fcmToken = token
-
 
278
                   appData.save()
-
 
279
                   
-
 
280
                   syncAdapter.sync {
-
 
281
                           success in
318
 
Línea 282... Línea 319...
282
                   }
319
        let userInfo = ["token": token]