2

I am registering my app for remote notifications using below code,

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

    let notificationSettings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: categories)
    application.registerUserNotificationSettings(notificationSettings)
    application.registerForRemoteNotifications()
    return true
}

func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
    print("token: \(deviceToken)")
}

func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {
    print("Error : Fail to register to APNS : \(error)")
}

For iPhone 6, It is not calling the method didRegisterForRemoteNotificationsWithDeviceToken or didFailToRegisterForRemoteNotificationsWithError, for other devices like 5c/4s it is being registered and printing deviceToken

Initially I thought it was OS issue but then, I had checked with other device with same version 9.3.2, It worked.

Did anyone face the same issue?

kumar
  • 481
  • 3
  • 7
  • 18
  • Are you sure you gave permission for notifications in that specific iPhone? – ujell Apr 27 '16 at 09:50
  • yes. I had given permissions. I even re-checked the same from settings and everything is granted. – kumar Apr 27 '16 at 09:58
  • Please check http://stackoverflow.com/questions/26817810/didregisterforremotenotificationswithdevicetoken-never-called-on-specific-device – Ayath Khan Apr 28 '16 at 11:52

4 Answers4

2

Write this code in didFinishLaunchingWithOptions for remote notificatios.Ithink It will work fine

if #available(iOS 8.0, *) {

    let userNotificationTypes:UIUserNotificationType = ([UIUserNotificationType.Alert, UIUserNotificationType.Badge, UIUserNotificationType.Sound])

    if UIApplication.sharedApplication().respondsToSelector("isRegisteredForRemoteNotifications") {


        // iOS 8 Notifications
        let settings:UIUserNotificationSettings = UIUserNotificationSettings(forTypes: userNotificationTypes, categories: nil)
        UIApplication.sharedApplication().registerUserNotificationSettings(settings)
        UIApplication.sharedApplication().registerForRemoteNotifications()


    } else {

        // iOS < 8 Notifications

        UIApplication.sharedApplication().registerForRemoteNotificationTypes([.Badge, .Sound, .Alert])

    }
    print("Push notifications setup complete")

}

Happy coding.

2

This was a problem from Apple side, not a problem with our code or devices.

As of today, everything is working again as expected. I was having the same problem as you since yesterday. 2 days ago everything was fine and I haven't made any changes to the code. After calling registerForRemoteNotifications, neither didRegisterForRemoteNotificationsWithDeviceToken nor didFailToRegisterForRemoteNotificationsWithError gets called.

I did a full restore to my iPhone 6 Plus iOS 9.3.1, but it didn't help. I also tried it on iPhone 5 iOS 9.3.1, still same problem.

I made a new sample application with different Apple ID, provisioning profile etc. STILL had the exact same problem with all the devices.

Matt
  • 74,352
  • 26
  • 153
  • 180
Roland T.
  • 841
  • 9
  • 16
  • I'm performing a full restore, and will see. – kumar Apr 27 '16 at 11:12
  • In fact everything was fine till yesterday for me too. But, from today itself this issue is being occurred. Not sure, what exactly the issue is all about – kumar Apr 27 '16 at 11:15
  • So I tried with iPhone 5 with iOS 8 and it works. Seems like it is a problem with Apple servers and iOS 9. 3 iPhones with iOS 9 and different projects/Apple ID-s doesn't work but iPhone with iOS 8 works. I suggest just to wait and see if this gets fixed. – Roland T. Apr 27 '16 at 11:35
  • I have tried with iPhone 5c with iOS 9.3.1 and it works. I hope this issue is with devices like iPhone 6. Yeah, Let wait and see – kumar Apr 27 '16 at 11:44
  • It works now with iPhone 6 and 9.3.1. Checkout my answer. – kumar Apr 27 '16 at 11:56
2

Not sure what exactly the issue is, After restoring the iPhone and changing didFinishLaunchingWithOptions as below. It worked.

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

    let notificationSettings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)
    application.registerUserNotificationSettings(notificationSettings)
    application.registerForRemoteNotifications()
    return true
}
kumar
  • 481
  • 3
  • 7
  • 18
1

On first launch you will be asked with "*** would like to send you notifications". If you tap on Don't allow, the methods

func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
print("token: \(deviceToken)")
}

func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {
print("Error : Fail to register to APNS : \(error)")
}

will not get called. But this is different in another scenario. If you tap on OK when asked to allow notifications and then turn off notifications in settings, the notification delegates will be called, but will not deliver notification.

Ayath Khan
  • 213
  • 2
  • 10
  • I'm clicking on OK itself and not turning off the notifications from settings. – kumar Apr 27 '16 at 09:59
  • Please check whether you have turned off in settings – Ayath Khan Apr 27 '16 at 10:00
  • I have checked and is still ON – kumar Apr 27 '16 at 10:01
  • Delete the app and reinstall and check if this persists. – Ayath Khan Apr 27 '16 at 10:02
  • I have tried by deleting the app and hard resetting the device, even restarted xcode. Still the same. – kumar Apr 27 '16 at 10:03
  • This is hard man. Really hard. I suggest you test this on another iPhone 6 with iOS 9.3.2 if possible. Hard to figure out the issue. Or you can try a new sample project and debug that on this device. – Ayath Khan Apr 27 '16 at 10:06
  • I will restore the device and try it once again. I feel like that is the only solution as of now. Thanks for the support and suggestion. – kumar Apr 27 '16 at 10:08
  • 1
    Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/110364/discussion-between-ayath-khan-and-kumar). – Ayath Khan Apr 27 '16 at 10:08
  • I found a similar thread http://stackoverflow.com/questions/26817810/didregisterforremotenotificationswithdevicetoken-never-called-on-specific-device this might be helpful for you – Ayath Khan Apr 28 '16 at 11:54
  • After restoring my device, It started working. Not sure what exactly the issue was. – kumar Apr 28 '16 at 12:05
  • Maybe that worked for you. but we have to find the exact reason. So that it will help other developers. So please find the cause and post it here. – Ayath Khan Apr 28 '16 at 12:09