如何快速接收数据通知(FCM)

我需要迅速接收数据通知,并且不使用通知消息。仅使用数据消息。

这是我给邮递员的杰森

{

"to": "e32uafd....","data": {
        "urlImage": "https://upload.wikimedia.org/wikipedia/commons/8/80/image.jpg","title": "title","body": "****body****.","mutable_content": true
    }
}

这是我的快速代码

@available(iOS 10.0,*)
    func userNotificationCenter(_ center: UNUserNotificationCenter,willPresent notification: UNNotification,withCompletionHandler   completionHandler: @escaping (_ options:   UNNotificationPresentationOptions) -> Void)
    {
                switch UIApplication.shared.applicationState
                    {
                    case .active:
                        completionHandler( [.alert,.sound])
                        break
                    default:
                        completionHandler( [.alert,.sound])
                        break
                          }
                             }
        }   
  }
Z20070230326 回答:如何快速接收数据通知(FCM)

您可以使用func application(_ application: UIApplication,didReceiveRemoteNotification userInfo: [AnyHashable : Any],fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void)获取通知数据,但是在进行通知或注册UNUserNotificationCenter时,必须先使ViewController成为didFinishLaunchWithOptions(:)的委托。

第1步 让您的AppDelegate成为UNNotificationCenter的代表

 func application(_ application: UIApplication,didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

     // Assumming you have imported NotificationCenter
     UNUserNotificationCenter.current().delegate = self
    //......

        return true
    }


第2步 获取您的信息

func application(_ application: UIApplication,fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {

     print(userInfo) // Your notification info
    // Note that you have to cast/parse it depending on which data you want to handle. 


    }

本文链接:https://www.f2er.com/3148261.html

大家都在问