APNS - 获取设备令牌

时间:2010-03-06 16:15:26

标签: iphone apple-push-notifications

我将使用AppNotify服务在我的应用中实现推送通知。要完成该服务的设置,我需要获取我的设备令牌。我尝试过使用Apple的文档中的代码。在模拟器上我得到一个错误(当然是预期的)。在我的设备上,我没有收到错误,但我也没有得到令牌。都没有调用委托方法。这是代码(第一位在applicationDidFinishLaunching中):

    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];



- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)devToken {
//const void *devTokenBytes = [devToken bytes];
//self.registered = YES;
//[self sendProviderDeviceToken:devTokenBytes]; // custom method
NSLog(@"Success");
NSLog(@"Token = %@", devToken);
}
- (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err {
    NSLog(@"Error in registration. Error: %@", err);
}

为什么会发生这种情况?

由于

2 个答案:

答案 0 :(得分:2)

有一次,您必须知道远程通知在模拟器上不起作用。

此致

答案 1 :(得分:2)

还添加以下方法:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo 
{
       for (id key in userInfo)
       {
             NSLog(@"key: %@, value: %@", key, [userInfo objectForKey:key]);
        }    
}

如果您已经编写了正确的代码(如您所示),则会调用上述3种方法中的一种(您编写的2种方法和上面的方法)。如果您没有在设备上收到任何错误,那么等待一段时间(理想情况下为10秒,但也可能需要更长时间)才能调用didRegisterForRemoteNotificationsWithDeviceToken方法。问候。