From 68c43af4e99eeb32a20b7411e0c5d660651fd7b9 Mon Sep 17 00:00:00 2001 From: Matte23 Date: Fri, 4 Dec 2020 11:01:57 +0100 Subject: [PATCH] Add base support for notifications --- ios/circolapp/circolapp/AppDelegate.swift | 42 +++++++++++++++++++ .../circolapp/Model/iOSServerApi.swift | 22 ++++++++-- .../circolapp/shared/server/ServerAPI.kt | 4 ++ 3 files changed, 65 insertions(+), 3 deletions(-) diff --git a/ios/circolapp/circolapp/AppDelegate.swift b/ios/circolapp/circolapp/AppDelegate.swift index 89dfbcb..8d76015 100644 --- a/ios/circolapp/circolapp/AppDelegate.swift +++ b/ios/circolapp/circolapp/AppDelegate.swift @@ -17,12 +17,24 @@ */ import UIKit +import UserNotifications import Firebase @UIApplicationMain class AppDelegate: NSObject, UIApplicationDelegate { func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool { FirebaseApp.configure() + + // For iOS 10 display notification (sent via APNS) + UNUserNotificationCenter.current().delegate = self + + let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound] + UNUserNotificationCenter.current().requestAuthorization( + options: authOptions, + completionHandler: {_, _ in }) + + application.registerForRemoteNotifications() + return true } @@ -38,3 +50,33 @@ class AppDelegate: NSObject, UIApplicationDelegate { // Use this method to release any resources that were specific to the discarded scenes, as they will not return. } } + +extension AppDelegate : UNUserNotificationCenterDelegate { + + // Receive displayed notifications for iOS 10 devices. + func userNotificationCenter(_ center: UNUserNotificationCenter, + willPresent notification: UNNotification, + withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { + iOSRepository.getCircularRepository().updateCirculars(returnNewCirculars: false, completionHandler: + { result, error in + if let errorReal = error { + print(errorReal.localizedDescription) + } + }) + + // UI is updated automatically + } + + func userNotificationCenter(_ center: UNUserNotificationCenter, + didReceive response: UNNotificationResponse, + withCompletionHandler completionHandler: @escaping () -> Void) { + iOSRepository.getCircularRepository().updateCirculars(returnNewCirculars: false, completionHandler: + { result, error in + if let errorReal = error { + print(errorReal.localizedDescription) + } + }) + + completionHandler() + } +} diff --git a/ios/circolapp/circolapp/Model/iOSServerApi.swift b/ios/circolapp/circolapp/Model/iOSServerApi.swift index 8a82ff7..0596411 100644 --- a/ios/circolapp/circolapp/Model/iOSServerApi.swift +++ b/ios/circolapp/circolapp/Model/iOSServerApi.swift @@ -17,19 +17,21 @@ */ import Foundation +import Firebase import Shared class iOSServerApi { static let instance = iOSServerApi() - private let key = "school" + private let schoolKey = "school" + private let topicKey = "topic" private let serverCompanion = ServerAPI.Companion() var serverAPI: ServerAPI private var userDefaultsObserver: NSKeyValueObservation? = nil init() { - let serverID = UserDefaults.standard.integer(forKey: key) + let serverID = UserDefaults.standard.integer(forKey: schoolKey) let server = serverCompanion.getServer(serverID: Int32(serverID)) serverAPI = ServerAPI(serverName: server) @@ -43,10 +45,24 @@ class iOSServerApi { } func changeServer(serverID: Int) { - let serverID = UserDefaults.standard.integer(forKey: key) + let serverID = UserDefaults.standard.integer(forKey: schoolKey) let server = serverCompanion.getServer(serverID: Int32(serverID)) serverAPI = ServerAPI(serverName: server) + let nullableOldTopic = UserDefaults.standard.string(forKey: topicKey) + let newTopic = serverCompanion.getServerTopic(serverID: Int32(serverID)) + if (nullableOldTopic == nil || nullableOldTopic != newTopic) { + if let oldTopic = nullableOldTopic { + Messaging.messaging().unsubscribe(fromTopic: oldTopic + "IOS") + } + + Messaging.messaging().subscribe(toTopic: newTopic + "IOS") { error in + print("Subscribed to topic: " + newTopic) + } + + UserDefaults.standard.set(newTopic, forKey: topicKey) + } + CircularRepository(circularDao: iOSRepository.getCircularDao(), serverAPI: serverAPI).updateCirculars(returnNewCirculars: false, completionHandler: { result, error in if let errorReal = error { diff --git a/shared/src/commonMain/kotlin/net/underdesk/circolapp/shared/server/ServerAPI.kt b/shared/src/commonMain/kotlin/net/underdesk/circolapp/shared/server/ServerAPI.kt index c1f576f..98a975e 100644 --- a/shared/src/commonMain/kotlin/net/underdesk/circolapp/shared/server/ServerAPI.kt +++ b/shared/src/commonMain/kotlin/net/underdesk/circolapp/shared/server/ServerAPI.kt @@ -64,6 +64,10 @@ class ServerAPI(serverName: Servers) { return Servers.values()[serverID] } + fun getServerTopic(serverID: Int): String { + return getServer(serverID).toString() + } + fun getServerId(server: Servers): Int { return Servers.values().indexOf(server) }