Gopal Devra
Gopal Devra

Reputation: 171

Device token not generating iOS

I am facing issue while generating the device token with the real device. I am debugging device and device token is not generating. Sometimes it works and sometimes not.

Please let me know what could be issue. Thanks

Upvotes: 1

Views: 1852

Answers (4)

J. Lopes
J. Lopes

Reputation: 1345

@GopalDevra could you be more clear? Anyway, I have this code for Parse, maybe it's not your case, but you can get the idea.

You can use didRegisterForRemoteNotificationsWithDeviceToken in AppDelegate.m like:

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {

    // Store the deviceToken in the current installation and save it to Parse.
    PFInstallation *currentInstallation = [PFInstallation currentInstallation];
    [currentInstallation setDeviceTokenFromData:deviceToken];
    currentInstallation.channels = @[ @"global" ];
    [currentInstallation saveInBackground];
}

Edited

Have you put this in didFinishLaunchingWithOptions?

// - Push notifications
// -- Register for Push Notitications
UIUserNotificationType userNotificationTypes = (UIUserNotificationTypeAlert |
                                                UIUserNotificationTypeBadge |
                                                UIUserNotificationTypeSound);
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:userNotificationTypes
                                                                         categories:nil];

[application registerUserNotificationSettings:settings];
[application registerForRemoteNotifications];

//-- Set Notification
if ([application respondsToSelector:@selector(isRegisteredForRemoteNotifications)])
{
    // iOS 8 Notifications
    [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];

    [application registerForRemoteNotifications];
}
else
{
    // iOS < 8 Notifications
    [application registerForRemoteNotificationTypes:
     (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound)];        
}

//--- your custom code

Upvotes: 0

CheeseLemon
CheeseLemon

Reputation: 126

As of now, I, too, am having trouble getting device tokens.

My projects stopped generating device tokens when it was "erased" and "installed" around 10 hours ago. Also in the Korean iOS Developers forum, some people have been reporting problems with APNS tokens not generating in the past 10 hours.

There may be something wrong with some of the sandbox APNS servers.

  • Last checked time 2016-04-27 22:43 PM +0900 GMT : No device token, Push Notification Not Arriving.

Upvotes: 1

Anuj J
Anuj J

Reputation: 186

Add this method,

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

You will get answer, why token is not generated.

Upvotes: 0

Fahim Parkar
Fahim Parkar

Reputation: 31647

Did you put below in didFinishLaunchingWithOptions of AppDelegate.m?

if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
{
    NSLog(@"ios8 app");
    [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
    [[UIApplication sharedApplication] registerForRemoteNotifications];
}
else
{
    NSLog(@"lower ios8 app");
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];
}

Upvotes: 1

Related Questions