7

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

}

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

4

5 回答 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)

}
于 2016-03-01T14:46:32.173 回答
5

Swift 4.2 - 称之为

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
    debugPrint("Received: \(userInfo)")
    completionHandler(.newData)
}
于 2018-11-10T08:20:21.593 回答
0

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

于 2016-03-01T14:49:03.140 回答
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)

    }
于 2017-02-23T11:08:20.197 回答
0

相当老的帖子,但可以在我的帖子中找到解决方案Push Notifications are Delivered but didReceiveRemoteNotification is never called Swift or here didReceiveRemoteNotification function does not call with FCM notification server。检查此参数:"content_available": true在您的通知定义或推送通知服务中。下划线是应该如何设置的。

于 2019-05-30T17:00:13.517 回答