didReceiveRemoteNotification

didReceiveRemoteNotification

本文介绍了斯威夫特didReceiveRemoteNotification不被调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用oneSignal作为推送提供程序的应用程序.我可以收到推送通知,效果很好.但是,如果我尝试访问推送有效载荷,则不会得到任何结果,因为didReceiveRemoteNotification没有被调用.

I have an app with oneSignal as push provider. I can receive push notifications, that work good. But if I try to access push payload I get nothing as didReceiveRemoteNotification not called.

我有以下代码

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")

}

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

Problem is that nothing prints out when I receive push. What am I doing wrong ?

推荐答案

在此位置尝试一次delegete方法

try once your delegete method in this place

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

称呼这个人

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

    print("Recived: \(userInfo)")

    completionHandler(.NewData)

}

这篇关于斯威夫特didReceiveRemoteNotification不被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-21 20:58