mirror of
https://github.com/Matte23/circolapp.git
synced 2025-12-06 07:29:10 +00:00
Add base support for notifications
This commit is contained in:
@@ -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()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user