推送通知设备令牌?

14 iphone objective-c token apple-push-notifications ios

如何从我的iPhone设备获取设备令牌?

Use*_*321 14

此方法将在调试模式下在控制台中打印deviceToken,如果您想查看UIAlert中也可以看到的设备令牌.

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    NSLog(@"APN device token: %@", deviceToken);
    NSString *deviceTokenString = [NSString stringWithFormat:@"%@",deviceToken];
    UIAlertView *deviceTokenAlert = [[UIAlertView alloc] initWithTitle:@"Device Token"
                                                            message:deviceTokenString
                                                           delegate:self
                                                  cancelButtonTitle:@"OK"
                                                  otherButtonTitles:nil];

}
Run Code Online (Sandbox Code Playgroud)

  • 感谢您的回答 !你知道这个设备令牌是否会随着时间的推移而改变,对于iPhone,还是不变? (2认同)

Rah*_*rma 7

如果您已实现此方法

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

}
Run Code Online (Sandbox Code Playgroud)

对于推送通知,您将获得设备令牌(此方法实际上是您在应用程序中实现的两种方法之一)

这可能会发现它很有用http://urbanairship.com/docs/push.html

您还可以在Iphone应用程序中查看推送通知

希望这个对你有帮助.


Aks*_*hay 6

此方法将在控制台中显示您的设备令牌.

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

    NSString *str = [NSString 
                     stringWithFormat:@"%@",deviceToken];
    NSString *newString = [str stringByReplacingOccurrencesOfString:@" " withString:@""];
    newString = [newString stringByReplacingOccurrencesOfString:@"<" withString:@""];
    newString = [newString stringByReplacingOccurrencesOfString:@">" withString:@""];


    [[NSUserDefaults standardUserDefaults] setObject:newString forKey:@"deviceToken"];



    NSLog(@"Your deviceToken ---> %@",newString);

}
Run Code Online (Sandbox Code Playgroud)