vijay
vijay

Reputation: 1235

APNS device token is getting generated incorrect

I have integrated apple push notification and facing a strange problem in my application. when I directly install the application via Xcode through usb connection then the device token is being generate stored in database correctly and push notification is working fine. but when I create IPA and install the app via created ipa in the same device then the device token is getting generated wrong and push notification is not working. Below is my code:

if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {
    [[UIApplication sharedApplication] registerUserNotificationSettings:      [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
} else {
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:
                             (UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)];
}

application.applicationIconBadgeNumber = 0;

#ifdef __IPHONE_8_0
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings
        *)notificationSettings {
    [application registerForRemoteNotifications];
}
#endif


- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData
    *)deviceToken {
      const unsigned *tokenData = deviceToken.bytes;
                    NSString *deviceTokenString = [NSString stringWithFormat:@"%08x%08x%08x%08x%08x%08x%08x%08x", ntohl(tokenData[0]),ntohl(tokenData[1]),ntohl(tokenData[2]),ntohl(tokenData[3]),ntohl(tokenData[4]),ntohl(tokenData[5]),ntohl(tokenData[6]),ntohl(tokenData[7])];
     [[NSUserDefaults standardUserDefaults]setObject:deviceTokenString forKey:@"devicetoken"];
      NSLog(@"Device Token = %@", deviceTokenString);
}

//Failed to Register for Remote Notifications
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
    NSLog(@"Error in registration. Error: %@", error);
}

Upvotes: 0

Views: 1063

Answers (1)

Christian
Christian

Reputation: 4641

The device Token depends on the certificate you signed your application with. If you install directly it is the dev-certificate, while when signing for AdHoc it is a distribution certificate. For push you need a corresponding distribution or development certificate packed on your server.

Upvotes: 2

Related Questions