user2252092
user2252092

Reputation: 61

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?

Upvotes: 1

Views: 3215

Answers (2)

Nick
Nick

Reputation: 2613

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

Upvotes: 1

Toam
Toam

Reputation: 362

This method will be called even if the user refuse to receive push notification. You can user UIRemoteNotificationType to get the user choice.

- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
{
    NSLog(@"My token is: %@", deviceToken);

    UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
    if (types == UIRemoteNotificationTypeNone){
        NSLog(@"User refused to receive push notifications");
    }else{
        NSLog(@"User agreed to receive push notifications");
    }
}

For your second question, you can't get the device token from iOS outside of this callback.

Upvotes: 0

Related Questions