获取示例设备令牌

时间:2013-05-03 03:27:42

标签: ios apple-push-notifications

我正试图从我的iPhone获取设备令牌。在stackoverflow中阅读帖子后,我已经完成了这个。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
 {
     [[UIApplication sharedApplication] registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeAlert ];
}
  (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken 
{
    NSLog(@"didRegisterForRemoteNotificationsWithDeviceToken: %@", deviceToken);
}

我在didRegisterForRemoteNotificationsWithDeviceToken中放了一个断点,但它并没有停在那里。我如何确保它被调用?

对不起这个noob问题..需要一些指导......

2 个答案:

答案 0 :(得分:1)

可能有任何理由。

要么在生成.cer文件时做错了,要么在配置应用程序以进行推送通知服务后删除旧配置时,未在XCode中安装新配置。

因此,在为苹果推送通知服务配置应用程序时,请再次在配置门户中进行检查。并再次下载.cer文件。 然后从.cer文件生成.pem文件(它包含私钥)。 然后再次下载新的配置文件,然后从XCode中删除旧的配置并安装新的配置。

然后使用:

#pragma mark -
#pragma mark Push Notifications
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)_deviceToken {
    // Get a hex string from the device token with no spaces or < >

    NSString *deviceToken = [[NSString alloc] init];

    deviceToken = [[[[_deviceToken description]
                     stringByReplacingOccurrencesOfString: @"<" withString: @""]
                    stringByReplacingOccurrencesOfString: @">" withString: @""]
                   stringByReplacingOccurrencesOfString: @" " withString: @""];

    NSLog(@"deviceToken = %@",deviceToken);

  }

希望它对你有所帮助。

答案 1 :(得分:0)

您必须为应用程序创建新的配置文件,并使用相同的方式为您的应用程序签名。您必须在设备中运行应用程序才能获取设备令牌。如果您在模拟器中运行应用程序,它将返回错误。

完成给定的教程

http://www.raywenderlich.com/3443/apple-push-notification-services-tutorial-part-12

http://developer.apple.com/library/ios/#documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Introduction.html