0

I'm making this sweet app that requires Push notifications.

However when trying to grab the Push token not all required functions seem to get executed.

The user does get presented with this Alert:

enter image description here

When the user hits OK I step through my code and see that not everything gets executed:

override func viewDidLoad(){
    let tapper = UITapGestureRecognizer(target: view, action:#selector(UIView.endEditing))
    tapper.cancelsTouchesInView = false
    view.addGestureRecognizer(tapper)
    
    print("gets called")
    registerForPushNotifications(UIApplication.sharedApplication())
}

func registerForPushNotifications(application: UIApplication) {
    print("gets called")
    let notificationSettings = UIUserNotificationSettings(
        forTypes: [.Badge, .Sound, .Alert], categories: nil)
    application.registerUserNotificationSettings(notificationSettings)
}

func application(application: UIApplication, didRegisterUserNotificationSettings notificationSettings: UIUserNotificationSettings) {
    print("doesn't get called")
    if notificationSettings.types != .None {
        application.registerForRemoteNotifications()
    }
}

func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
    print("doesn't get called")
    let tokenChars = UnsafePointer<CChar>(deviceToken.bytes)
    var apnsTokenString = ""
    
    for i in 0..<deviceToken.length {
        apnsTokenString += String(format: "%02.2hhx", arguments: [tokenChars[i]])
    }
    
    Constant.pushToken = apnsTokenString
    
    print("Device Token:", Constant.pushToken)
}

func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {
    debugPrint("Error Registering Remote Notification")
}

In my console the following gets printed:

gets called

gets called

Meaning that not all required functions get called. What am I doing wrong?

Community
  • 1
  • 1
Rutger Huijsmans
  • 2,330
  • 2
  • 30
  • 67
  • Not calling didRegisterForRemoteNotificationsWithDeviceToken func right ? Refer this Link - http://stackoverflow.com/questions/39490605/push-notification-issue-with-ios-10/39506524#39506524 – Sapana Ranipa Nov 11 '16 at 04:38
  • Nope, that one does NOT get called. And that is exactly the function that I need to get called. – Rutger Huijsmans Nov 11 '16 at 04:39
  • okay, Follow the step which given in this link - http://stackoverflow.com/questions/39490605/push-notification-issue-with-ios-10/39506524#39506524 – Sapana Ranipa Nov 11 '16 at 04:40
  • I've tried but not really helpful. I'm programming for iOS 9+ so I cannot use that framework. – Rutger Huijsmans Nov 11 '16 at 04:52

2 Answers2

1

Assuming all of the code you posted in your question is in your view controller class, the problem is that you need to put the UIApplicationDelegate methods in your actual app delegate class, not the view controller class. Simply move those methods to the proper class and they will work.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
-1

Just use this code

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {


    print("gets called")
    let settings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)

          application.registerUserNotificationSettings(settings)
          application.registerForRemoteNotifications()
}



func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {

          let characterSet: NSCharacterSet = NSCharacterSet(charactersInString: "<>")

          let deviceTokenString: String = (deviceToken.description as NSString)
               .stringByTrimmingCharactersInSet(characterSet)
               .stringByReplacingOccurrencesOfString( " ", withString: "") as String
         print(deviceTokenString)
     }
Moin Shirazi
  • 4,372
  • 2
  • 26
  • 38