如何获取推送通知的iPhone设备令牌?

时间:2012-05-09 07:00:21

标签: iphone notifications push

我正在尝试在我的应用程序中使用远程推送通知,我试图在我的应用程序中进行此测试,但是在我开始点击“允许”后我无法重置警报弹出窗口。< / p>

所以我的问题是:

即使用户点击提示弹出窗口中的“不允许”,我仍然可以获得设备令牌吗?

3 个答案:

答案 0 :(得分:3)

使用appDelegate方法

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
    {
        self.mDeviceToken = deviceToken;

        //Removing the brackets from the device token
        NSString *tokenString = [[deviceToken description] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];

        NSLog(@"Push Notification tokenstring is %@",tokenString);

    }   

以防万一错误

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{

    NSString* s=[[NSString alloc] initWithFormat:@"%@",error];
    UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Error" message:s delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
    [alert release];
    [s release];
// lert because your device will not show log
}

答案 1 :(得分:0)

使用以下代理方法...

-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{

    NSLog(@">>%@",deviceToken);// this will give  you token
}

-(void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error{

    NSLog(@">>%@",error); // this will gave you error msg with description.

}

希望,这会对你有帮助..

答案 2 :(得分:0)

-(void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken

{

    NSLog(@"My token is: %@", deviceToken);

}

通过这种方式,您可以获得iPhone设备令牌