1

我不知道如何在里面获取设备令牌 (APN)

- (BOOL)application:didFinishLaunchingWithOptions:

我可以在 AppDelegate 中获取设备令牌的唯一地方是

- (void)application:didRegisterForRemoteNotificationsWithDeviceToken:

其中 application:didRegisterForRemoteNotificationsWithDeviceToken 将异步运行或在 didFinishLaunchingWithOptions 之后运行。那么,有没有办法在 didFinishLaunchingWithOptions 中获取设备令牌?因为我想将它传递到从 didFinishLaunchingWithOptions 推送的 ViewController 中。

这是我的示例代码:

- (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];
}

非常感谢您的帮助。

4

1 回答 1

2

application:didRegisterForRemoteNotificationsWithDeviceToken:是您获得当前设备令牌的唯一机会。但是,您可以将结果存储NSUserDefaults并在启动时从那里读取。大多数情况下,如果应用程序尚未卸载/重新安装,则设备令牌在两次启动之间保持不变。

于 2012-04-27T02:34:41.040 回答