-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAppDelegate.swift
392 lines (331 loc) · 14.8 KB
/
AppDelegate.swift
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
//
// AppDelegate.swift
// SendBird-iOS
//
// Created by Jed Gyeong on 10/3/18.
// Copyright © 2018 SendBird. All rights reserved.
//
import UIKit
import UserNotifications
import SendBirdSDK
import AVKit
import AVFoundation
import Alamofire
import AlamofireImage
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {
var window: UIWindow?
var receivedPushChannelUrl: String?
var pushReceivedGroupChannel: String?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// New init with option to use local caching or not.
SBDMain.connect(withUserId: "", accessToken: "", apiHost: "", wsHost: "", completionHandler: nil)
SBDMain.initWithApplicationId("9DA1B1F4-0BE6-4DA8-82C5-2E81DAB56F23", useCaching: true, migrationStartHandler: {
print( "Application: Called when there's an upgrade in db.")
}, completionHandler: { error in
if error != nil {
print("Application: Called when initialize failed.")
} else {
print("Application: Called when initialize is done.")
}
})
self.registerForRemoteNotification()
SBDMain.setAppGroup("group.com.sendbird.sample4");
DataRequest.addAcceptableImageContentTypes(["binary/octet-stream"])
UINavigationBar.appearance().tintColor = UIColor(named: "color_navigation_tint")
let audioSession = AVAudioSession.sharedInstance()
do {
try audioSession.setCategory(AVAudioSession.Category.playback, mode: AVAudioSession.Mode.default, options: AVAudioSession.CategoryOptions.defaultToSpeaker)
} catch {
print("Setting category to AVAudioSessionCategoryPlayback failed.")
}
return true
}
@available(iOS 13.0, *)
func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
connectingSceneSession.userInfo?["activity"] = options.userActivities.first?.activityType
return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
}
@available(iOS 13.0, *)
func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
}
func applicationWillResignActive(_ application: UIApplication) {
}
func applicationDidEnterBackground(_ application: UIApplication) {
}
func applicationWillEnterForeground(_ application: UIApplication) {
}
func applicationDidBecomeActive(_ application: UIApplication) {
}
func applicationWillTerminate(_ application: UIApplication) {
}
// MARK: - Notification for Foreground mode
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler([.alert, .sound])
}
func registerForRemoteNotification() {
if self.compareVersions(version1: UIDevice.current.systemVersion, version2: "10.0") >= 0 {
#if !targetEnvironment(simulator)
let center = UNUserNotificationCenter.current()
center.delegate = self
center.requestAuthorization(options: [.alert, .sound]) { (granted, error) in
if granted == true {
UNUserNotificationCenter.current().getNotificationSettings(completionHandler: { (settings) in
if settings.authorizationStatus != UNAuthorizationStatus.authorized {
return
}
DispatchQueue.main.async {
UIApplication.shared.registerForRemoteNotifications()
}
})
}
}
return
#else
let center = UNUserNotificationCenter.current()
center.delegate = self
center.requestAuthorization(options: [.alert]) { (granted, error) in
}
#endif
}
else {
#if !targetEnvironment(simulator)
if UIApplication.shared.responds(to: #selector(UIApplication.registerUserNotificationSettings(_:))) {
let notificationSettings = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
UIApplication.shared.registerUserNotificationSettings(notificationSettings)
DispatchQueue.main.async {
UIApplication.shared.registerForRemoteNotifications()
}
}
#endif
}
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
SBDMain.registerDevicePushToken(deviceToken, unique: true) { (status, error) in
if error == nil {
if status == SBDPushTokenRegistrationStatus.pending {
print("Push registration is pending.")
}
else {
print("APNS Token is registered.")
}
}
else {
print("APNS registration failed with error: \(String(describing: error))")
}
}
}
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
print("Failed to get token, error: \(String(describing: error))")
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
if let sendbirdDict = userInfo["sendbird"] as? [String:Any] {
if let channelDict = sendbirdDict["channel"] as? [String:Any] {
self.pushReceivedGroupChannel = channelDict["channel_url"] as? String
}
}
}
func channel(_ sender: SBDBaseChannel, didReceive message: SBDBaseMessage) {
let topViewController = UIViewController.currentViewController()
if topViewController is GroupChannelsViewController {
return
}
if let vc = topViewController as? GroupChannelChatViewController {
if vc.channel?.channelUrl == sender.channelUrl {
return
}
}
guard let groupChannel = sender as? SBDGroupChannel else { return }
let pushOption = groupChannel.myPushTriggerOption
switch pushOption {
case .all, .default, .mentionOnly:
break
case .off:
return
@unknown default:
break
}
// Do Not Disturb - Need to implement as a function
var startHour = 0
var startMin = 0
var endHour = 0
var endMin = 0
var isDoNotDisturbOn = false
if UserDefaults.standard.value(forKey: "sendbird_dnd_start_hour") != nil {
startHour = UserDefaults.standard.value(forKey: "sendbird_dnd_start_hour") as! Int
}
else {
startHour = -1
}
if UserDefaults.standard.value(forKey: "sendbird_dnd_start_min") != nil {
startMin = UserDefaults.standard.value(forKey: "sendbird_dnd_start_min") as! Int
}
else {
startMin = -1
}
if UserDefaults.standard.value(forKey: "sendbird_dnd_end_hour") != nil {
endHour = UserDefaults.standard.value(forKey: "sendbird_dnd_end_hour") as! Int
}
else {
endHour = -1
}
if UserDefaults.standard.value(forKey: "sendbird_dnd_end_min") != nil {
endMin = UserDefaults.standard.value(forKey: "sendbird_dnd_end_min") as! Int
}
else {
endMin = -1
}
if UserDefaults.standard.value(forKey: "sendbird_dnd_on") != nil {
isDoNotDisturbOn = UserDefaults.standard.value(forKey: "sendbird_dnd_on") as! Bool
}
if startHour != -1 && startMin != -1 && endHour != -1 && endMin != -1 && isDoNotDisturbOn {
let date = Date()
let calendar = Calendar.current
let components = calendar.dateComponents([.hour, .minute], from: date)
let hour = components.hour
let minute = components.minute
let convertedStartMin = startHour * 60 + startMin
let convertedEndMin = endHour * 60 + endMin
let convertedCurrentMin = hour! * 60 + minute!
if convertedStartMin <= convertedEndMin && convertedStartMin <= convertedCurrentMin && convertedEndMin >= convertedCurrentMin {
return
}
else if convertedStartMin > convertedEndMin && (convertedStartMin < convertedCurrentMin || convertedEndMin > convertedCurrentMin) {
return
}
}
var title = ""
var body = ""
var type = ""
var customType = ""
if message is SBDUserMessage {
let userMessage = message as! SBDUserMessage
let sender = userMessage.sender
type = "MESG"
body = String(format: "%@: %@", (sender?.nickname)!, userMessage.message)
customType = userMessage.customType!
}
else if message is SBDFileMessage {
let fileMessage = message as! SBDFileMessage
let sender = fileMessage.sender
if fileMessage.type.hasPrefix("image") {
body = String(format: "%@: (Image)", (sender?.nickname)!)
}
else if fileMessage.type.hasPrefix("video") {
body = String(format: "%@: (Video)", (sender?.nickname)!)
}
else if fileMessage.type.hasPrefix("audio") {
body = String(format: "%@: (Audio)", (sender?.nickname)!)
}
else {
body = String(format: "%@: (File)", sender!.nickname!)
}
}
else if message is SBDAdminMessage {
let adminMessage = message as! SBDAdminMessage
title = ""
body = adminMessage.message
}
let content = UNMutableNotificationContent()
content.title = title
content.body = body
content.sound = UNNotificationSound.default
content.categoryIdentifier = "SENDBIRD_NEW_MESSAGE"
content.userInfo = [
"sendbird": [
"type": type,
"custom_type": customType,
"channel": [
"channel_url": sender.channelUrl
],
"data": "",
],
]
let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: 0.1, repeats: false)
let request = UNNotificationRequest(identifier: String(format: "%@_%@", content.categoryIdentifier, sender.channelUrl), content: content, trigger: trigger)
let center = UNUserNotificationCenter.current()
center.add(request) { (error) in
if error != nil {
}
}
}
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
guard let userInfo = response.notification.request.content.userInfo as? [String : Any] else { return }
guard let sendbirdDict = userInfo["sendbird"] as? [String:Any] else { return }
guard let channelDict = sendbirdDict["channel"] as? [String:Any] else { return }
guard let channelUrl = channelDict["channel_url"] as? String else { return }
self.pushReceivedGroupChannel = channelUrl
ConnectionManager.login { user, error in
if user != nil {
if self.pushReceivedGroupChannel != nil {
if let vc = UIViewController.currentViewController() {
vc.dismiss(animated: false) {
self.jumpToGroupChannel(self.pushReceivedGroupChannel)
}
}
else {
let viewController = UIStoryboard(name: "main", bundle: nil).instantiateViewController(withIdentifier: "MainTabBarViewController")
self.window = UIWindow(frame: UIScreen.main.bounds)
if let window = self.window {
window.rootViewController = viewController
window.makeKeyAndVisible()
}
self.jumpToGroupChannel(self.pushReceivedGroupChannel)
}
self.pushReceivedGroupChannel = nil
} else {
let viewController = UIStoryboard(name: "main", bundle: nil).instantiateInitialViewController()
self.window = UIWindow(frame: UIScreen.main.bounds)
if let window = self.window {
window.rootViewController = viewController
window.makeKeyAndVisible()
}
}
}
}
completionHandler()
}
func application(_ application: UIApplication, handleEventsForBackgroundURLSession identifier: String, completionHandler: @escaping () -> Void) {
completionHandler()
}
func jumpToGroupChannel(_ channelUrl: String?) -> Void {
if let vc = UIViewController.currentViewController() as? NotificationDelegate, let url = channelUrl{
vc.openChat(url)
}
}
// Swift version only.
private func compareVersions(version1: String, version2: String) -> Int {
var ret: Int = 0
let v1:[Int] = version1.split(separator: ".").map { (substring) -> Int in
return Int(substring)!
}
let v2 = version2.split(separator: ".").map { (substring) -> Int in
return Int(substring)!
}
let cntv1 = v1.count
let cntv2 = v2.count
let mincnt = cntv1 < cntv2 ? cntv1 : cntv2
for i in 0..<mincnt {
if v1[i] == v2[i] {
ret = 0
continue
}
else if v1[i] > v2[i] {
ret = 1
}
else {
ret = -1
}
break
}
if ret == 0 {
if cntv1 > cntv2 {
ret = 1
}
else if cntv1 < cntv2 {
ret = -1
}
}
return ret
}
}