8

I have an app with oneSignal as push provider. I can receive push notifications, that work good. But if I try to access push payload I get nothing as didReceiveRemoteNotification not called.

I have following code

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")

}

Problem is that nothing prints out when I receive push. What am I doing wrong ?

Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
Alexey K
  • 6,537
  • 18
  • 60
  • 118

5 Answers5

7

try once your delegete method in this place

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {

call this one

 func application(application: UIApplication,  didReceiveRemoteNotification userInfo: [NSObject : AnyObject],  fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {

    print("Recived: \(userInfo)")

    completionHandler(.NewData)

}
Kendall Helmstetter Gelner
  • 74,769
  • 26
  • 128
  • 150
Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
6

Swift 4.2 - call this

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
    debugPrint("Received: \(userInfo)")
    completionHandler(.newData)
}
Abhishek Jain
  • 4,557
  • 2
  • 32
  • 31
1

Use this code for 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)

    }
Manab Kumar Mal
  • 20,788
  • 5
  • 31
  • 43
0

Hey @Alexey make sure your app provisioning file has enabled for Push Notification Service

Avinash Jadhav
  • 491
  • 4
  • 17
0

Pretty old post but solutions can be found here in my post Push Notifications are delivered but didReceiveRemoteNotification is never called Swift or here didReceiveRemoteNotification function doesn't called with FCM notification server. Check for this parameter: "content_available": truein your notification definition or push notification service. With underscore is how it should be set.

Vincenzo
  • 5,304
  • 5
  • 38
  • 96