Swift read userInfo of remote notification

后端 未结 7 1630
庸人自扰
庸人自扰 2020-12-08 02:11

I implemented a function to open an AlertView when I receive a remote notification like this:

func application(application: UIApplication, didReceiveRemoteNo         


        
7条回答
  •  生来不讨喜
    2020-12-08 02:55

    Alert should be showing while the app is in active state. So check the state is active or not.

    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
        if application.applicationState == .active {
          if let aps = userInfo["aps"] as? NSDictionary {
            if let alertMessage = aps["alert"] as? String {
              let alert = UIAlertController(title: "Notification", message: alertMessage, preferredStyle: UIAlertControllerStyle.alert)
              let action = UIAlertAction(title: "Ok", style: .default, handler: nil)
              alert.addAction(action)
              self.window?.rootViewController?.present(alert, animated: true, completion: nil)
            }
          }
        }
        completionHandler(.newData)
      }
    

    From this if a user need message then he can get alert message.

提交回复
热议问题