Swift didReceiveRemoteNotification没有调用

Ale*_*y K 5 ios swift onesignal

我有一个使用oneSignal作为推送提供商的应用程序.我可以收到推送通知,效果很好.但是,如果我尝试访问推送有效负载,我什么都didReceiveRemoteNotification没有被调用.

我有以下代码

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

 if application.applicationState != UIApplicationState.Background {

        let preBackgroundPush = !application.respondsToSelector("backgroundRefreshStatus")
        let oldPushHandlerOnly = !self.respondsToSelector("application:didReceiveRemoteNotification:fetchCompletionHandler:")
        var pushPayload = false
        if let options = launchOptions {
            pushPayload = options[UIApplicationLaunchOptionsRemoteNotificationKey] != nil
        }

    }
    if application.respondsToSelector("registerUserNotificationSettings:") {
        let settings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)
        application.registerUserNotificationSettings(settings)
        application.registerForRemoteNotifications()
    } else {
        let types : UIRemoteNotificationType =  [.Badge, .Alert, .Sound]
        application.registerForRemoteNotificationTypes(types)
    }

}

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {

    if userInfo["d"] as! String == "t"  {

        print("key was received")

    }
    print(userInfo)
    print("PREVED")

}
Run Code Online (Sandbox Code Playgroud)

问题是当我收到推送时没有打印出来.我究竟做错了什么 ?

Anb*_*hik 6

在这个地方尝试一次你的delegete方法

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
Run Code Online (Sandbox Code Playgroud)

叫这一次

 func application(application: UIApplication,  didReceiveRemoteNotification userInfo: [NSObject : AnyObject],  fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {

    print("Recived: \(userInfo)")

    completionHandler(.NewData)

}
Run Code Online (Sandbox Code Playgroud)


Abh*_*ain 5

Swift 4.2 - 调用这个

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
    debugPrint("Received: \(userInfo)")
    completionHandler(.newData)
}
Run Code Online (Sandbox Code Playgroud)