Question

I don't know how to get device token (APN) inside

- (BOOL)application:didFinishLaunchingWithOptions:

the only place I can get the device token in AppDelegate is in

- (void)application:didRegisterForRemoteNotificationsWithDeviceToken:

where application:didRegisterForRemoteNotificationsWithDeviceToken will run asynchronous or after didFinishLaunchingWithOptions. So, is there any way to get device token in didFinishLaunchingWithOptions?? Because I want to pass it into my ViewController that pushed from didFinishLaunchingWithOptions.

Here is my sample code:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    for (id key in launchOptions) {
        NSLog(@"Key: %@, Value %@", key, [launchOptions objectForKey: key]);
    }
    AddViewController *avc = [[AddViewController alloc] init];
    avc.managedObjectContext = self.managedObjectContext;
    avc.pushToken = self.pushToken;
    UINavigationController *uinav = [[UINavigationController alloc] init ];

    [uinav pushViewController:avc animated:YES];
    [_window addSubview:uinav.view];
    [avc release];

    [self.window makeKeyAndVisible];

    // Let the device know we want to receive push notifications
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:
     (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
    return YES;
}

- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
{
    NSLog(@"My token is: %@", deviceToken);
    self.pushToken = [NSString stringWithFormat:@"%@", deviceToken];
}

Thanks a lot for any help.

Was it helpful?

Solution

application:didRegisterForRemoteNotificationsWithDeviceToken: is your one and only chance to get the current device token. However, you can store the result in NSUserDefaults and read it from there on launch. Most of the time, if the app hasn't been uninstalled/reinstalled, the device token remains the same between launches.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top