如果应用程序处于终止状态,则处理点击通知 iOS
Handle tap on notification if App is in killed state iOS
我正在使用 firebase 推送通知,当应用处于后台和前台状态时,一切正常 运行。但是当应用程序处于终止状态时,点击通知会转到主页。我已经使用了所有可能的方法 - 通过使用启动选项检查
if let remoteNotification = launchOptions?[.remoteNotification] as? [AnyHashable : Any] {
if let userInfo = remoteNotification as? [String : Any]
{
DispatchQueue.main.asyncAfter(deadline: .now()+1.0, execute: {
UIApplication.shared.applicationIconBadgeNumber = 0
let notificationManager = PushNotificationManager()
notificationManager.handleNotificationAction(data: userInfo)
})
}
}
我不知道我错过了什么。请帮忙
In your appdelegate add this extension.
extension AppDelegate: UNUserNotificationCenterDelegate {
func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler
completionHandler: @escaping () -> Void) {
if UserDefaults.standard.bool(forKey: "login_session") {
let mainStoryboardIpad : UIStoryboard = UIStoryboard(name: "Main",
bundle: nil)
let initialViewControlleripad : UIViewController =
mainStoryboardIpad.instantiateViewController(withIdentifier: "Notifs_Main") as UIViewController
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.rootViewController = initialViewControlleripad
self.window?.makeKeyAndVisible()
} else {
UserDefaults.standard.set(false, forKey: "isLoggedin")
print("failed")
}
return completionHandler()
}
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent
notification: UNNotification, withCompletionHandler completionHandler:
@escaping (UNNotificationPresentationOptions) -> Void) {
return completionHandler(UNNotificationPresentationOptions.alert)
}
}
我正在使用 firebase 推送通知,当应用处于后台和前台状态时,一切正常 运行。但是当应用程序处于终止状态时,点击通知会转到主页。我已经使用了所有可能的方法 - 通过使用启动选项检查
if let remoteNotification = launchOptions?[.remoteNotification] as? [AnyHashable : Any] {
if let userInfo = remoteNotification as? [String : Any]
{
DispatchQueue.main.asyncAfter(deadline: .now()+1.0, execute: {
UIApplication.shared.applicationIconBadgeNumber = 0
let notificationManager = PushNotificationManager()
notificationManager.handleNotificationAction(data: userInfo)
})
}
}
我不知道我错过了什么。请帮忙
In your appdelegate add this extension.
extension AppDelegate: UNUserNotificationCenterDelegate {
func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler
completionHandler: @escaping () -> Void) {
if UserDefaults.standard.bool(forKey: "login_session") {
let mainStoryboardIpad : UIStoryboard = UIStoryboard(name: "Main",
bundle: nil)
let initialViewControlleripad : UIViewController =
mainStoryboardIpad.instantiateViewController(withIdentifier: "Notifs_Main") as UIViewController
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.rootViewController = initialViewControlleripad
self.window?.makeKeyAndVisible()
} else {
UserDefaults.standard.set(false, forKey: "isLoggedin")
print("failed")
}
return completionHandler()
}
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent
notification: UNNotification, withCompletionHandler completionHandler:
@escaping (UNNotificationPresentationOptions) -> Void) {
return completionHandler(UNNotificationPresentationOptions.alert)
}
}