Swift didReceiveRemoteNotification未被称为

时间:2016-03-01 14:00:29

标签: ios swift onesignal

我有一个appSignal作为推送提供商。我可以收到推送通知,效果很好。但是,如果我尝试访问推送有效负载,则无法调用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")

}

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

5 个答案:

答案 0 :(得分:6)

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

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)

}

答案 1 :(得分:2)

Swift 4.2 -调用

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

答案 2 :(得分:0)

嗨@Alexey确保您的应用配置文件已启用推送通知服务

答案 3 :(得分:0)

将此代码用于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)

    }

答案 4 :(得分:-1)

帖子很旧,但是可以在我的帖子Push Notifications are delivered but didReceiveRemoteNotification is never called SwiftdidReceiveRemoteNotification function doesn't called with FCM notification server中找到解决方案。 在通知定义或推送通知服务中检查以下参数:"content_available": true。下划线是应该如何设置。