user4790024
user4790024

Reputation:

Device token not generating for some devices?

I notice some devices doesn't generate device token for push notification. I have been using the following code to generate device token and implemented the didRegisterForRemoteNotificationsWithDeviceToken method

        var types: UIUserNotificationType = UIUserNotificationType.Badge |
            UIUserNotificationType.Alert |
            UIUserNotificationType.Sound
        var settings: UIUserNotificationSettings = UIUserNotificationSettings( forTypes: types, categories: nil )
        application.registerUserNotificationSettings( settings )
        application.registerForRemoteNotifications()

I get device token for some devices.However i thought device token was not generated for some devices due to lack of internet connnection.To be sure, i turned off the internet connection and made sure that was my problem but it successfully generates token?

In my knowledge my app requests the device token with the apple server...

Why is this happening.And how is device token generated?

Do i really dont need internet connection to generate device token

Upvotes: 3

Views: 2914

Answers (4)

user10165565
user10165565

Reputation:

If your application has previously registered, calling registerForRemoteNotificationTypes: results in the operating system passing the device token to the delegate immediately without incurring additional overhead.

Otherwise Change WIFI

Hope it helps.

Upvotes: 1

Lucas Farah
Lucas Farah

Reputation: 1092

Add the method:

func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) 
{
  print("application:didFailToRegisterForRemoteNotificationsWithError: %@", error)
}

So you can find out what the error is. I already had similar problems with that.

Upvotes: 0

baydi
baydi

Reputation: 1003

Interent connection is must to register device for remotenotification

var types: UIUserNotificationType = UIUserNotificationType.Badge |
        UIUserNotificationType.Alert |
        UIUserNotificationType.Sound
    var settings: UIUserNotificationSettings = UIUserNotificationSettings( forTypes: types, categories: nil )
    application.registerUserNotificationSettings( settings )
    application.registerForRemoteNotifications()

above code only work when you have internet

What you can do when is when u got device token in this function didRegisterForRemoteNotificationsWithDeviceToken

store it in NSUserDefault , Now next time check if you have internet connection than do the above method if not get device token from NSUserDefault. To check internetConnection you can use Reachability class got it from here https://github.com/ashleymills/Reachability.swift

Upvotes: 3

atulkhatri
atulkhatri

Reputation: 11333

Make sure you didn't accidentally click 'Don't allow' when push notification permission dialog was shown. If that is the case, you will have to go to the settings and enable push notifications for your app manually and then you will get the device token.

For the internet connection issue: Requesting device token does require internet connection in order to register your device to APNS. But once you have registered your device, every other subsequent calls will give you the device token without requiring internet connection as it doesn't access the APNS server:

You can have a look at it:

By requesting the device token and passing it to the provider every time your application launches, you help to ensure that the provider has the current token for the device. If a user restores a backup to a device or computer other than the one that the backup was created for (for example, the user migrates data to a new device or computer), he or she must launch the application at least once for it to receive notifications again. If the user restores backup data to a new device or computer, or reinstalls the operating system, the device token changes. Moreover, never cache a device token and give that to your provider; always get the token from the system whenever you need it. If your application has previously registered, calling registerForRemoteNotificationTypes: results in the operating system passing the device token to the delegate immediately without incurring additional overhead.

Upvotes: 0

Related Questions