Proyectos de Subversion Iphone Microlearning - Nuevo Interface

Rev

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

Rev 11 Rev 17
Línea 14... Línea 14...
14
 
14
 
15
class AppDelegate : NSObject, UIApplicationDelegate {
15
class AppDelegate : NSObject, UIApplicationDelegate {
16
    private var isSyncInProgress = false
16
    private var isSyncInProgress = false
17
    private var syncAdapter = SyncAdapter()
17
    private var syncAdapter = SyncAdapter()
18
    private let gcmMessageIDKey = "gcm.message_id"
18
    private let gcmMessageIDKey = "gcm.message_id"
-
 
19
    private var appData = AppData.sharedInstance
Línea 19... Línea 20...
19
    private let appDao = AppDao.sharedInstance
20
 
Línea 20... Línea 21...
20
 
21
 
21
    static var orientationLock = UIInterfaceOrientationMask.portrait
22
    static var orientationLock = UIInterfaceOrientationMask.portrait
Línea 30... Línea 31...
30
        let database = Database.sharedInstance
31
        let database = Database.sharedInstance
31
        if database.open() != nil {
32
        if database.open() != nil {
32
              database.createTables()
33
              database.createTables()
33
        }
34
        }
Línea -... Línea 35...
-
 
35
        
-
 
36
        if let new_uuid = UIDevice.current.identifierForVendor?.uuidString {
34
        
37
           
-
 
38
            let old_uuid = appData.deviceUuid
35
        let appDao = AppDao.sharedInstance
39
            if new_uuid != old_uuid {
-
 
40
                var sync = SyncModel()
-
 
41
                sync.type = Constants.SYNC_ADAPTER_TYPE_DEVICE
36
        var appData = appDao.selectOne()
42
                sync.data = new_uuid
-
 
43
                
-
 
44
                if SyncDao.sharedInstance.insert(record: sync) > 0 {
-
 
45
                    appData.deviceUuid = new_uuid
37
   
46
                    appData.save()
38
        
47
                    
39
        if appData.id == 0 {
48
                    syncAdapter.sync {
40
            appData.deviceUuid = "-"
49
                            success in
-
 
50
                    }
41
            appDao.insert(model: appData)
51
                }
42
        }
-
 
Línea 43... Línea -...
43
        
-
 
44
 
-
 
45
        
-
 
46
        print("registro del dispositivo")
-
 
47
        NotificationCenter.default.addObserver(self, selector: #selector(receivingNewDevice(n:)), name: Constants.NOTIFICATION_NAME_REGISTER_NEW_DEVICE, object: nil)
-
 
48
              
-
 
49
        if let uuid = UIDevice.current.identifierForVendor?.uuidString {
-
 
50
            let userInfo = ["uuid": uuid]
52
            }
Línea 51... Línea -...
51
            NotificationCenter.default.post(name: Constants.NOTIFICATION_NAME_REGISTER_NEW_DEVICE , object: self, userInfo: userInfo)
-
 
52
        }
53
 
53
              
-
 
54
    
-
 
55
 
-
 
56
        NotificationCenter.default.addObserver(self, selector: #selector(receivingNewToken(n:)), name: Constants.NOTIFICATION_NAME_REGISTER_NEW_FCM_TOKEN, object: nil)
-
 
57
              
-
 
58
        // Remote Notification Enable// Use Firebase library to configure APIs
-
 
59
              
54
        }
60
        
-
 
61
        print("Firebase configuracion")
-
 
62
        FirebaseApp.configure()
-
 
63
              
-
 
64
        UNUserNotificationCenter.current().delegate = self
-
 
65
        let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
-
 
66
        UNUserNotificationCenter.current().requestAuthorization(
-
 
67
            options: authOptions,
-
 
Línea 68... Línea -...
68
            completionHandler: {_, _ in }
-
 
69
        )
-
 
70
              
55
              
71
 
56
  
-
 
57
        FirebaseApp.configure()
72
              
58
 
73
        
59
                Messaging.messaging().delegate = self
74
        Messaging.messaging().delegate = self
60
 
-
 
61
                if #available(iOS 10.0, *) {
-
 
62
                  // For iOS 10 display notification (sent via APNS)
-
 
63
                  UNUserNotificationCenter.current().delegate = self
-
 
64
 
-
 
65
                  let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
-
 
66
                  UNUserNotificationCenter.current().requestAuthorization(
-
 
67
                    options: authOptions,
-
 
68
                    completionHandler: {_, _ in })
-
 
69
                } else {
75
        
70
                  let settings: UIUserNotificationSettings =
-
 
71
                  UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
-
 
72
                  application.registerUserNotificationSettings(settings)
76
        print("Register For Remote Notifications")
73
                }
Línea 77... Línea -...
77
        application.registerForRemoteNotifications()
-
 
Línea 78... Línea 74...
78
        
74
 
Línea -... Línea 75...
-
 
75
                application.registerForRemoteNotifications()
-
 
76
                return true
-
 
77
 
-
 
78
         
-
 
79
 
-
 
80
 
-
 
81
    }
-
 
82
    
-
 
83
    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
-
 
84
                         fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
-
 
85
 
Línea -... Línea 86...
-
 
86
        if let messageID = userInfo[gcmMessageIDKey] {
Línea 79... Línea -...
79
        
-
 
80
        
-
 
-
 
87
            print("Message ID: \(messageID)")
Línea 81... Línea 88...
81
         
88
        }
82
 
-
 
83
        return true;
-
 
84
 
-
 
85
    }
-
 
86
    
-
 
87
    
-
 
88
    
-
 
89
    // Storage of the UID once the notification is received
-
 
90
    @objc func receivingNewDevice(n: NSNotification){
89
        
91
 
90
 
92
            print("receivingNewDevice")
-
 
93
            if n.userInfo != nil{
-
 
94
                if let new_uuid = n.userInfo?["uuid"]! as? String {
-
 
95
                    var appData = appDao.selectOne()
-
 
96
                    let old_uuid = appData.deviceUuid
-
 
97
                    if new_uuid != old_uuid {
-
 
98
                        var sync = SyncModel()
91
        processNotification(userInfo: userInfo, isForeground: false)
99
                        sync.type = Constants.SYNC_ADAPTER_TYPE_DEVICE
-
 
100
                        sync.data = new_uuid
-
 
101
                        
92
        completionHandler(UIBackgroundFetchResult.newData)
102
                        if SyncDao.sharedInstance.insert(record: sync) > 0 {
93
    }
103
                            appData.deviceUuid = new_uuid
-
 
104
                            
-
 
105
                            print("update query : 18")
-
 
106
                            appDao.update(model: appData)
-
 
Línea 107... Línea -...
107
                            
-
 
108
      
-
 
109
 
-
 
110
                            syncAdapter.sync {
-
 
111
                                    success in
-
 
-
 
94
    
112
                            }
95
 
113
                        }
96
    
114
                    }
97
}
115
                }
98
 
116
            }
99
extension AppDelegate: MessagingDelegate {
117
        }
100
    
118
        
101
    func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String?) {
119
        @objc func receivingNewToken(n: NSNotification){
102
 
120
            if n.userInfo != nil{
103
        let token = fcmToken ?? ""
121
                if let token = n.userInfo?["token"]! as? String {
-
 
122
                    if !token.isEmpty {
-
 
123
                        var appData = appDao.selectOne()
104
        print("token: ", token)
124
                        if appData.fcmToken != token  {
105
        
125
                            
106
 
126
                            var sync = SyncModel()
-
 
127
                            sync.data = token
107
        if appData.fcmToken != token  {
128
                            sync.type = Constants.SYNC_ADAPTER_TYPE_FCM
-
 
129
                            
-
 
130
                            if SyncDao.sharedInstance.insert(record: sync) > 0 {
-
 
131
                            
108
            
132
                                appData.fcmToken = token
109
            var sync = SyncModel()
133
                                
110
            sync.data = token
-
 
111
            sync.type = Constants.SYNC_ADAPTER_TYPE_FCM
134
                                print("update query : 19")
112
            
Línea 135... Línea -...
135
                                appDao.update(model: appData)
-
 
136
                                
113
            if SyncDao.sharedInstance.insert(record: sync) > 0 {
137
                                syncAdapter.sync {
114
            
-
 
115
                appData.fcmToken = token
138
                                        success in
116
                appData.save()
139
                                }
117
                
140
                            }
118
                syncAdapter.sync {
141
                        }
119
                        success in
142
                    }
120
                }
Línea -... Línea 121...
-
 
121
            }
143
                }
122
        }
-
 
123
    }
Línea 144... Línea 124...
144
            }
124
}
145
        }
-
 
146
}
-
 
147
 
125
 
148
//Recibir mensajes
126
@available(iOS 10, *)
149
@available(iOS 10, *)
-
 
150
extension AppDelegate: UNUserNotificationCenterDelegate {
-
 
Línea -... Línea 127...
-
 
127
extension AppDelegate : UNUserNotificationCenterDelegate {
151
  // Receive displayed notifications for iOS 10 devices.
128
 
152
  func userNotificationCenter(_ center: UNUserNotificationCenter,
129
  // Receive displayed notifications for iOS 10 devices.
153
                              willPresent notification: UNNotification,
130
  func userNotificationCenter(_ center: UNUserNotificationCenter,
154
                              withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions)
-
 
155
                                -> Void) {
131
                              willPresent notification: UNNotification,
-
 
132
    withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
Línea -... Línea 133...
-
 
133
    let userInfo = notification.request.content.userInfo
-
 
134
    
-
 
135
    if let messageID = userInfo[gcmMessageIDKey] {
156
    
136
        print("Message ID: \(messageID)")
-
 
137
    }
-
 
138
    
-
 
139
    processNotification(userInfo: userInfo, isForeground: true)
-
 
140
    completionHandler([[.banner, .badge, .sound]])
-
 
141
  }
Línea 157... Línea -...
157
    print("message userNotificationCenter iOS 10")
-
 
158
    
142
    
Línea -... Línea 143...
-
 
143
    private func processNotification(userInfo : [AnyHashable : Any], isForeground : Bool) {
-
 
144
        let title = userInfo["title"]  as? String ?? ""
-
 
145
        let body = userInfo["body"]  as? String ?? ""
159
    let userInfo = notification.request.content.userInfo
146
        let url = userInfo["url"] as? String ?? ""
-
 
147
        let command = userInfo["command"] as? String ?? ""
-
 
148
        let new_capsules = Int(userInfo["new_capsules"] as? String ?? "") ?? 0
Línea 160... Línea 149...
160
 
149
 
161
    // With swizzling disabled you must let Messaging know about the message, for Analytics
-
 
162
    // Messaging.messaging().appDidReceiveMessage(userInfo)
150
        
-
 
151
        if command == "signout" {
163
 
152
            NotificationCenter.default.post(name: Constants.NOTIFICATION_NAME_COMMAND_EXIT , object: self, userInfo: nil)
164
    // ...
153
        }
-
 
154
        if command == "content-refresh" {
165
    print(userInfo)
155
            let userinfo = ["new_capsules" : String("new_capsules"), "is_foreground" : (isForeground ? "1" : "0")]
166
    
-
 
167
    if let aps = userInfo["aps"] as? [String:Any] {
156
            
168
      //let badgeNumber = aps["badge"] as! Int
-
 
169
      //application.applicationIconBadgeNumber = badgeNumber
157
            NotificationCenter.default.post(name: Constants.NOTIFICATION_NAME_COMMAND_REFRESH_CONTENT , object: self, userInfo: userinfo)
170
        
-
 
Línea -... Línea 158...
-
 
158
        }
-
 
159
        
-
 
160
        
-
 
161
        if !title.isEmpty && !body.isEmpty {
-
 
162
            
-
 
163
            var userNotification = UserNotificationModel()
-
 
164
            userNotification.user_uuid = appData.userUuid
-
 
165
            userNotification.title = title
-
 
166
            userNotification.description = body
171
        //print("aps: \(aps)")
167
            userNotification.url = url
-
 
168
            userNotification.viewed = 0
172
 
169
            
-
 
170
            let userNotificationDao = UserNotificationDao.sharedInstance
-
 
171
            userNotificationDao.insert(userNotification: userNotification)
-
 
172
            
-
 
173
        }
-
 
174
    }
-
 
175
 
-
 
176
    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
-
 
177
 
173
        
178
            print( "didRegisterForRemoteNotificationsWithDeviceToken")
174
        //if let alert = aps["alert"]as? [String:Any] {
179
        
Línea 175... Línea -...
175
        
-
 
176
           // let title = alert["title"] ?  alert["title"]!  : ""
-
 
177
           // let body = alert["body"] ? alert["body"]! : ""
-
 
178
            
-
 
179
            //if !title.isEmpty  && !body.isEmpty {
-
 
180
            
-
 
181
            //    var userNotification = UserNotificationModel()
-
 
182
            //    userNotification.title = title
-
 
183
            //    userNotification.description = body
-
 
184
            //}
-
 
185
        //}
-
 
186
        //let title = aps["title"] as! String
-
 
187
        //let body = aps["body"] as! String
-
 
188
        
-
 
189
        // Print full message.
-
 
190
        //print("userInfo title: \(title)")
-
 
Línea 191... Línea -...
191
        //print("userInfo body: \(body)")
-
 
192
        
180
            let tokenParts = deviceToken.map { data in String(format: "%02.2hhx", data) }
193
        if let new_capsules = userInfo["new_capsules"] as? Int {
181
            let token = tokenParts.joined()
194
              print("new_capsules : \(new_capsules)") // output: "some-value"
182
            print("token: ", token)
Línea 195... Línea 183...
195
           }
183
           
196
    }
184
 
197
    
185
           if appData.fcmToken != token  {
198
    /*
-
 
199
     message userNotificationCenter iOS 10
-
 
200
     [AnyHashable("aps"): {
-
 
201
         alert =     {
186
               
Línea -... Línea 187...
-
 
187
               var sync = SyncModel()
-
 
188
               sync.data = token
202
             body = "Cuerpo notificaci\U00f3n";
189
               sync.type = Constants.SYNC_ADAPTER_TYPE_FCM
203
             subtitle = "Sub t\U00edtulo notificaci\U00f3n";
190
               
204
             title = "T\U00edtulo notificaci\U00f3n";
-
 
205
         };
-
 
206
     }]
191
               if SyncDao.sharedInstance.insert(record: sync) > 0 {
207
     aps: ["alert": {
-
 
208
         body = "Cuerpo notificaci\U00f3n";
192
               
Línea 209... Línea 193...
209
         subtitle = "Sub t\U00edtulo notificaci\U00f3n";
193
                   appData.fcmToken = token
210
         title = "T\U00edtulo notificaci\U00f3n";
194
                   appData.save()
211
     }]
195
                   
212
     */
-
 
213
    
-
 
214
    
-
 
215
 
-
 
216
 
-
 
217
    // Change this to your preferred presentation option
-
 
218
    completionHandler([[.alert, .sound]])
-
 
219
  }
-
 
220
 
-
 
221
  func userNotificationCenter(_ center: UNUserNotificationCenter,
-
 
222
                              didReceive response: UNNotificationResponse,
-
 
223
                              withCompletionHandler completionHandler: @escaping () -> Void) {
-
 
224
    
-
 
225
    
-
 
226
    print("message userNotificationCenter")
-
 
227
    let userInfo = response.notification.request.content.userInfo
-
 
228
 
-
 
229
    // ...
-
 
230
 
-
 
231
    // With swizzling disabled you must let Messaging know about the message, for Analytics
-
 
232
    // Messaging.messaging().appDidReceiveMessage(userInfo)
-