如何在 iPhone 上请求设备令牌

发布于 2024-08-31 23:53:32 字数 390 浏览 2 评论 0原文

我可以在订阅推送通知时使用 didRegisterForRemoteNotificationWithDeviceToken 回调方法来获取我的 iPhone 的设备令牌。我的问题是我以后怎样才能再次获得这个令牌?当用户订阅我的应用程序中的某些内容时,我想发送设备令牌和他们订阅的项目的 id...但我不知道从哪里获取设备令牌。我尝试使用 UIDevice 类中的 uniqueIdentifer,但该值与原始令牌不同。我想我可以在每次我的应用程序开始生成令牌时调用 registerForRemoteNotificationTypes 。但如果我这样做,我不确定如何从不同的类访问该值(我的 didRegisterForRemoteNotificationWithDeviceToken 回调位于主应用程序委托中)。感谢您对 Objective C 新手的任何帮助!

I am able to use the didRegisterForRemoteNotificationWithDeviceToken callback method to get the device token of my iphone when subscribing to push notifications. My question is how can I get this token again a later time? When a user subscribes to something in my application, I want to send the device token and the id of the item they are subscribing to...but I can't figure out where to get the device token from. I tried using the uniqueIdentifer from the UIDevice class but this value is different than what the original token was. I supposed I could call registerForRemoteNotificationTypes each time my app starts to produce the token. But if I do that, I'm not sure how I can access this value from a different class (my didRegisterForRemoteNotificationWithDeviceToken callback is located in the main application delegate). Thanks for any help for an objective C newbie!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

因为看清所以看轻 2024-09-07 23:53:32

我会在您的 appDelegate 中设置一个可以从任何地方访问的属性,并将其设置为设备令牌。

// .h
@interface SomeAppDelegate : NSObject <UIApplicationDelegate> {
    NSString * dToken;
}

@property (nonatomic, retain) NSString * dToken;

// .m
@implementation SomeAppDelegate;
@synthesize dToken;

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

    NSString * token = [[NSString alloc] initWithData:deviceToken encoding:NSUTF8StringEncoding];
    [self setDToken:token];
    [token release];
}
- (void)dealloc {
    [dToken release]
    [super dealloc];
}

然后,您可以使用以下方法在任何地方访问该令牌:

NSString * token = [(SomeAppDelegate*)[[UIApplication sharedApplication] delegate] dToken];

I would set a property in your appDelegate that can be accessed from anywhere, and set it to the device token.

// .h
@interface SomeAppDelegate : NSObject <UIApplicationDelegate> {
    NSString * dToken;
}

@property (nonatomic, retain) NSString * dToken;

// .m
@implementation SomeAppDelegate;
@synthesize dToken;

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

    NSString * token = [[NSString alloc] initWithData:deviceToken encoding:NSUTF8StringEncoding];
    [self setDToken:token];
    [token release];
}
- (void)dealloc {
    [dToken release]
    [super dealloc];
}

You can then access that token anywhere by using:

NSString * token = [(SomeAppDelegate*)[[UIApplication sharedApplication] delegate] dToken];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文