Search code examples
ios6apple-push-notifications

Get device token if user refuse Push notification ios


I have set Push notification into my new app. I have heard it is not a good way to do but I get the device token using:

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


    NSString *deviceToken = [[newDeviceToken description] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];
    deviceToken = [deviceToken stringByReplacingOccurrencesOfString:@" " withString:@""];

    NSLog(@"###### DEVICE TOKEN = %@ #########",deviceToken); 


}

Everything is okay for me, nevertheless I use this token to log in a user into my base, but I have a problem: how can I get the device token if the user refuses to receive push notifications? How can I get the device token outside the App Delegate?


Solution

  • You cannot get the device token if the user does not agree to receive push notifications. The method

    - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
    

    will be called instead but you cannot get the token from that.

    If you're looking for a unique identifer, you should consider using the identifierForVendor. See the documentation here: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIDevice_Class/Reference/UIDevice.html#//apple_ref/occ/instp/UIDevice/identifierForVendor