代码之家  ›  专栏  ›  技术社区  ›  Alexey K

未调用Swift didReceiveRemoteNotification

  •  8
  • Alexey K  · 技术社区  · 10 年前

    我有一个以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")
    
    }
    

    问题是,当我收到推送时,什么都不会打印出来。我做错了什么?

    5 回复  |  直到 10 年前
        1
  •  7
  •   Kendall Helmstetter Gelner    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)
    
    }
    
        2
  •  6
  •   Abhishek Jain    7 年前

    银行代码4.2 -叫这个

    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
        debugPrint("Received: \(userInfo)")
        completionHandler(.newData)
    }
    
        3
  •  1
  •   Manab Kumar Mal    9 年前

    将此代码用于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
  •  0
  •   Avinash Jadhav    10 年前

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

        5
  •  0
  •   Vincenzo    7 年前

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