Я внедрил Firebase для моего iOS приложения.
Я получаю уведомление, но когда я нажимаю на него, он переходит к соответствующему V C, когда приложение работает
Мой код выглядит следующим образом:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
//Mark: FCM
FirebaseApp.configure()//for google analytics too
if #available(iOS 10.0, *) {
// 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 })
} else {
let settings: UIUserNotificationSettings =
UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
application.registerUserNotificationSettings(settings)
}
application.registerForRemoteNotifications()
Messaging.messaging().delegate = self
UserDefaults.standard.set(false, forKey: "Bubble")
// Override point for customization after application launch.
print("222222222")
let launchedBefore = UserDefaults.standard.bool(forKey: "launchedBefore")
if launchedBefore {
//print("Not first launch.")
//Check if already login
if let token = UserDefaults.standard.string(forKey: "token_key") {
if token != ""{
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let navigationController = appDelegate.window!.rootViewController as! UINavigationController
let destinationController = storyboard.instantiateViewController(withIdentifier: "LaunchVC") as? LaunchViewController
navigationController.pushViewController(destinationController!, animated: false)
}
}
} else {
//print("First launch, setting UserDefault.")
UserDefaults.standard.set(true, forKey: "launchedBefore")
// Mark : delete the token on first launch
// KeychainWrapper.standard.removeObject(forKey: "token_key")
//KeychainWrapper.standard.removeObject(forKey: "ContactId")
}
IQKeyboardManager.shared.enable = true
print("33333333")
return true
}
Но когда я убиваю свое приложение из фона и получаю уведомление pu sh, и я нажимаю на уведомление.
ЭТО БУДЕТ ... ....!
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
// TODO: Handle data of notification
// With swizzling disabled you must let Messaging know about the message, for Analytics
//Messaging.messaging().appDidReceiveMessage(userInfo)
print("44444444")
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let navigationController = appDelegate.window!.rootViewController as! UINavigationController
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let destinationController = storyboard.instantiateViewController(withIdentifier: "NotificationVC") as? NotificationViewController
navigationController.pushViewController(destinationController!, animated: false)
if let messageID = userInfo["aps"] {
print("Message ID: \(messageID)")
}
print("555555555555")
}
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler(UNNotificationPresentationOptions.alert)
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let navigationController = appDelegate.window!.rootViewController as! UINavigationController
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let destinationController = storyboard.instantiateViewController(withIdentifier: "NotificationVC") as? NotificationViewController
navigationController.pushViewController(destinationController!, animated: false)
completionHandler(UIBackgroundFetchResult.newData)
print("8888888")
}
Я не знаю, где я ошибаюсь, я пробовал возможное решение, как: 1. Изменение контроллера вида root 2. Экземпляр окна
Еще один метод ..
func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) {
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let navigationController = appDelegate.window!.rootViewController as! UINavigationController
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let destinationController = storyboard.instantiateViewController(withIdentifier: "NotificationVC") as? NotificationViewController
navigationController.pushViewController(destinationController!, animated: false)
}
Не смог дозвониться, Может кто-нибудь поделиться ..!
спасибо заранее ..