mirror of
https://github.com/Matte23/circolapp.git
synced 2025-12-05 23:19:10 +00:00
Add base support for notifications
This commit is contained in:
@@ -17,12 +17,24 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import UIKit
|
import UIKit
|
||||||
|
import UserNotifications
|
||||||
import Firebase
|
import Firebase
|
||||||
|
|
||||||
@UIApplicationMain
|
@UIApplicationMain
|
||||||
class AppDelegate: NSObject, UIApplicationDelegate {
|
class AppDelegate: NSObject, UIApplicationDelegate {
|
||||||
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
|
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
|
||||||
FirebaseApp.configure()
|
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
|
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.
|
// 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 Foundation
|
||||||
|
import Firebase
|
||||||
import Shared
|
import Shared
|
||||||
|
|
||||||
class iOSServerApi {
|
class iOSServerApi {
|
||||||
static let instance = iOSServerApi()
|
static let instance = iOSServerApi()
|
||||||
|
|
||||||
private let key = "school"
|
private let schoolKey = "school"
|
||||||
|
private let topicKey = "topic"
|
||||||
private let serverCompanion = ServerAPI.Companion()
|
private let serverCompanion = ServerAPI.Companion()
|
||||||
|
|
||||||
var serverAPI: ServerAPI
|
var serverAPI: ServerAPI
|
||||||
private var userDefaultsObserver: NSKeyValueObservation? = nil
|
private var userDefaultsObserver: NSKeyValueObservation? = nil
|
||||||
|
|
||||||
init() {
|
init() {
|
||||||
let serverID = UserDefaults.standard.integer(forKey: key)
|
let serverID = UserDefaults.standard.integer(forKey: schoolKey)
|
||||||
let server = serverCompanion.getServer(serverID: Int32(serverID))
|
let server = serverCompanion.getServer(serverID: Int32(serverID))
|
||||||
serverAPI = ServerAPI(serverName: server)
|
serverAPI = ServerAPI(serverName: server)
|
||||||
|
|
||||||
@@ -43,10 +45,24 @@ class iOSServerApi {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func changeServer(serverID: Int) {
|
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))
|
let server = serverCompanion.getServer(serverID: Int32(serverID))
|
||||||
serverAPI = ServerAPI(serverName: server)
|
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:
|
CircularRepository(circularDao: iOSRepository.getCircularDao(), serverAPI: serverAPI).updateCirculars(returnNewCirculars: false, completionHandler:
|
||||||
{ result, error in
|
{ result, error in
|
||||||
if let errorReal = error {
|
if let errorReal = error {
|
||||||
|
|||||||
@@ -64,6 +64,10 @@ class ServerAPI(serverName: Servers) {
|
|||||||
return Servers.values()[serverID]
|
return Servers.values()[serverID]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun getServerTopic(serverID: Int): String {
|
||||||
|
return getServer(serverID).toString()
|
||||||
|
}
|
||||||
|
|
||||||
fun getServerId(server: Servers): Int {
|
fun getServerId(server: Servers): Int {
|
||||||
return Servers.values().indexOf(server)
|
return Servers.values().indexOf(server)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user