Swift 不会打印来自通知委托的有效负载

Posted

技术标签:

【中文标题】Swift 不会打印来自通知委托的有效负载【英文标题】:Swift will not print payload from notification delegate 【发布时间】:2017-05-28 12:46:49 【问题描述】:

我无法打印收到远程通知的委托。我在物理设备上收到通知,但是当我尝试访问有效负载时没有任何反应。我导入import UserNotifications 我还链接了用户通知库,但这无助于我不确定我在错误期间发生了什么。我使用一个信号,但这不应该导致确实接收通知方法不起作用。

 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool 
    setupViewApDelegate()
    application.statusBarStyle = .lightContent

    //facebook login
    FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)

    //APIkeys().oneSignal
    let onesignalInitSettings = [kOSSettingsKeyAutoPrompt: false]
    OneSignal.initWithLaunchOptions(launchOptions,
                                    appId: APIkeys().oneSignal,
                                    handleNotificationAction: nil,
                                    settings: onesignalInitSettings)

    OneSignal.inFocusDisplayType = OSNotificationDisplayType.notification;
    OneSignal.promptForPushNotifications(userResponse:  accepted in
        print("User accepted notifications: \(accepted)")
    )


    return true

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) 
        print("Push notification received: \(userInfo)")
    

【问题讨论】:

OneSignal Not calling didReceiveRemoteNotification的可能重复 【参考方案1】:

看来我必须在 oneSignal 闭包中检索有效负载

OneSignal.initWithLaunchOptions(launchOptions, appId: APIkeys().oneSignal, handleNotificationReceived:              (notification) in
        let payload: OSNotificationPayload? = notification?.payload


        let fullMessage: String? = payload?.body

        print("Recived no" + fullMessage!)

        print("Received Notification - \(notification?.payload.notificationID) - \(notification?.payload.title)")

    , handleNotificationAction: nil, settings: [kOSSettingsKeyAutoPrompt : false, kOSSettingsKeyInAppAlerts : false])
    OneSignal.promptForPushNotifications(userResponse:  accepted in
        print("User accepted notifications: \(accepted)")
    )
    OneSignal.inFocusDisplayType = OSNotificationDisplayType.notification;

【讨论】:

以上是关于Swift 不会打印来自通知委托的有效负载的主要内容,如果未能解决你的问题,请参考以下文章

使用 Swift 脚本打印有效的 JSON

GCM 未在通知中使用来自 JSON 有效负载的数据

当应用程序在后台收到通知时如何获取通知有效负载数据

Swift字典包含值,但不会在调试器控制台上打印

Swift - 委托协议结构

当应用程序处于后台时,iOS 处理通知和有效负载