我在iOS应用程序中实现了Firebase。
我收到通知,但是当我单击它时,正在运行应用程序时将我带到相关的VC
我的代码如下:
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
}
但是当我从后台终止我的应用程序并收到推送通知时,我单击了通知。
它崩溃....!
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.更改根视图控制器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)
}
无法通过,任何人都可以分享..!
预先感谢..
我正面临着相同的行为。您能找到解决方法吗?