Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift didReceiveRemoteNotification not called

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 ?

like image 860
Alexey K Avatar asked Mar 01 '16 14:03

Alexey K


1 Answers

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)

}
like image 180
Anbu.Karthik Avatar answered Oct 08 '22 12:10

Anbu.Karthik