Swift的didReceiveRemoteNotification未被调用

8

我有一个使用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")

}

问题在于当我收到推送时,没有任何东西打印出来。我做错了什么吗?

委托方法是否被调用。 - Anbu.Karthik
委托方法没有被调用。 - Alexey K
registerUserNotificationSettings 已经从 10.0+ 版本开始被弃用。 - Sazzad Hissain Khan
5个回答

7

在这个位置尝试一下你的委托方法

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)

}

6

Swift 4.2 - 叫它

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

谢谢,使用这个解决了我的问题,我之前用的是一个过时的类似的东西... - DeyaEldeen

1
使用此代码适用于Swift 3.0。
//MARK: Push notification receive delegates

    func application(_ application: UIApplication,
                              didReceiveRemoteNotification userInfo: [AnyHashable : Any],
                              fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void)
    {

        print("Recived: \(userInfo)")

        completionHandler(.newData)

    }

0

嘿 @Alexey,请确保您的应用程序配置文件已启用推送通知服务


0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接