How to handle push notif that it will notify even when the app is open or closed. i have the ff. code below and its notifying when app is close but it does not notify when app is open. i have provided my source code below.Thank You. Help greatly appreciated.How to handle push notif that it will notify even when the app is open or closed. i have the ff. code below and its notifying when app is close but it does not notify when app is open. i have provided my source code below.Thank You. Help greatly appreciated.
import UIKit
import CoreData
import Firebase
import FirebaseMessaging
import UserNotifications
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
if UserDefaults.standard.bool(forKey: "isLoggedIn") {
let menuVC = storyBoard.instantiateViewController(withIdentifier: "MenuCollectionViewController") as! MenuCollectionViewController
let nvc: UINavigationController = UINavigationController(rootViewController: menuVC)
nvc.navigationBar.isHidden = true
self.window?.rootViewController = nvc
self.window?.makeKeyAndVisible()
} else {
self.showLoginScreen()
}
FirebaseApp.configure()
let notificationTypes : UIUserNotificationType = [UIUserNotificationType.alert , UIUserNotificationType.badge, UIUserNotificationType.sound]
let notificationSettings = UIUserNotificationSettings(types: notificationTypes,categories : nil)
application.registerForRemoteNotifications()
application.registerUserNotificationSettings(notificationSettings)
return true
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
// print("MessageID: \(userInfo["gcm_message_id"])")
// print(userInfo)
if let message = userInfo["gcm_message_id"] {
print("MessageID: \(message)")
}
print(userInfo)
}
func showLoginScreen()
{
UserDefaults.standard.set(false, forKey: "isLoggedIn")
UserDefaults.standard.synchronize()
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let loginViewController = storyboard.instantiateViewController(withIdentifier: "LoginViewController") as! LoginViewController
let nvc: UINavigationController = UINavigationController(rootViewController: loginViewController)
nvc.navigationBar.isHidden = true
self.window?.rootViewController = nvc
self.window?.makeKeyAndVisible()
}
func applicationWillResignActive(_ application: UIApplication) {
}
func applicationDidEnterBackground(_ application: UIApplication) {
}
func applicationWillEnterForeground(_ application: UIApplication) {
}
func applicationDidBecomeActive(_ application: UIApplication) {
}
func applicationWillTerminate(_ application: UIApplication) {
}
lazy var persistentContainer: NSPersistentContainer = {
let container = NSPersistentContainer(name: "FridgeBoard")
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
if let error = error as NSError? {
fatalError("Unresolved error \(error), \(error.userInfo)")
}
})
return container
}()
func saveContext () {
let context = persistentContainer.viewContext
if context.hasChanges {
do {
try context.save()
} catch {
let nserror = error as NSError
fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
}
}
}
}