【问题标题】:Handle tap on notification if App is in killed state iOS [duplicate]如果应用程序处于终止状态iOS,则处理点击通知[重复]
【发布时间】:2020-03-31 04:12:52
【问题描述】:

我正在使用 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)
        })
        }
    }

我不知道我错过了什么。请帮忙。

【问题讨论】:

  • 1. 你在哪里写的这段代码? 2.在上述场景中你想登陆哪个控制器?
  • 您阅读过UNUserNotificationCenterDelegate 文档吗?您可以通过单击 (⊞ +​​ 单击) 与此处联系。您将获得查询的解决方案。
  • 我正在编写此代码 App Delegate didFinishLaunchingWithOptions 方法并希望打开一个控制器,该控制器将由 handleNotification 方法 @dahiya_boy 的用户信息内容数据决定
  • 可以添加handleNotifiationAction方法的代码吗?
  • 是的,我已经更新了问题,请立即查看

标签: ios swift apple-push-notifications push


【解决方案1】:
   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)
   }
 }

【讨论】:

  • 我已经使用了这些方法,但是当应用程序处于终止状态时它们不起作用 -func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) { 如果让 userInfo = response.notification.request.content.userInfo 为? [String : Any] { UIApplication.shared.applicationIconBadgeNumber = 0 let notificationManager = PushNotificationManager() notificationManager.handleNotificationAction(data: userInfo) } completionHandler() }
  • 但是当应用程序在后台运行时您会收到通知吗?
  • 是的,当应用程序处于运行或后台状态时一切正常,这些委托方法在这种情况下被调用但不是处于终止状态,因此它打开主页
  • 我可以看看你的 didFinishLaunchingWithOptions 吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-08-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-23
  • 2016-05-05
  • 1970-01-01
相关资源
最近更新 更多