【问题标题】:How to get delivered notification details in appdelegate如何在 appdelegate 中获取传递的通知详细信息
【发布时间】:2018-11-07 04:07:11
【问题描述】:

当应用程序打开时,我已经设法通过点击通知获取通知详细信息,但如果用户通过单击应用程序图标打开应用程序,是否有任何方法可以在应用程序内获取传递的通知详细信息

func applicationWillResignActive(_ application: UIApplication) {

        if #available(iOS 10.0, *) {

            let center = UNUserNotificationCenter.current()
            center.getDeliveredNotifications { (notification) in
                print(notification.count) 

            }

        } else {
            // Fallback on earlier versions
        }

    }

在这里我得到了通知计数,但我不知道如何从这里获取通知的详细信息(用户信息)

【问题讨论】:

    标签: ios swift notifications appdelegate


    【解决方案1】:

    我找到了答案

    func applicationDidBecomeActive(_ application: UIApplication) {
            if #available(iOS 10.0, *) {
    
                let center = UNUserNotificationCenter.current()
    
    
                center.getDeliveredNotifications { (receivedNotifications) in
    
    
                    for notification in receivedNotifications {
                        let content = notification.request.content
                        print(" Body \(content.body)")
                        print(" Title \(content.title)")
                        print(content.userInfo as NSDictionary)
    
                        self.session.saveCallRequest(content.userInfo as NSDictionary)
    
                    }
    
                }
        }
    

    【讨论】:

      【解决方案2】:

      如果您的应用被强制关闭(向上滑动)并且用户点击通知打开应用,您仍然可以在AppDelegate didFinishLaunchingWithOptions 方法中获取此信息:

      func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
          // Override point for customization after application launch.
      
          if let userInfo = launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification] as? [String: AnyObject]{
              //Here you can use the notification payload information.
          }
      
          return true
      }
      

      【讨论】:

        猜你喜欢
        • 2014-03-25
        • 1970-01-01
        • 2014-10-22
        • 1970-01-01
        • 2016-09-28
        • 1970-01-01
        • 1970-01-01
        • 2017-09-24
        • 1970-01-01
        相关资源
        最近更新 更多