推送通知中的设备令牌

发布于 2025-01-02 01:16:13 字数 932 浏览 1 评论 0原文

我只想向某些用户发送推送通知。

根据我在苹果文档中所经历的内容。 注册推送通知的代码是这样的

- (void)applicationDidFinishLaunching:(UIApplication *)app {
   // other setup tasks here....
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];
}

// Delegation methods
- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)devToken {
    const void *devTokenBytes = [devToken bytes];
    self.registered = YES;
    [self sendProviderDeviceToken:devTokenBytes]; // custom method
}

- (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err {
    NSLog(@"Error in registration. Error: %@", err);
}

在方法 appdidRegisterForRemoteNotif 中。我只看到创建并发送到服务器的 devToken 字节。但是我如何识别哪个设备令牌属于哪个用户。因此,如果我的设备名称是 Shubhank 的 iPhone。我如何发送我的 iPhone 是这样的信息,这是我的设备令牌。

I want to send push notification to certain users only.

From what I have gone through in Apple docs.
The code to register for push notification is this

- (void)applicationDidFinishLaunching:(UIApplication *)app {
   // other setup tasks here....
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];
}

// Delegation methods
- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)devToken {
    const void *devTokenBytes = [devToken bytes];
    self.registered = YES;
    [self sendProviderDeviceToken:devTokenBytes]; // custom method
}

- (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err {
    NSLog(@"Error in registration. Error: %@", err);
}

In the method appdidRegisterForRemoteNotif..I see only devToken bytes created and send to the server..but how will I identify which device token belongs to which user. So if my device name is Shubhank's iPhone. How can I send the information that my iPhone is this and this is my device token.

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

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

发布评论

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

评论(3

幼儿园老大 2025-01-09 01:16:13

通常,您不会在委托方法本身中更新服务器上的 apns 令牌。您可以保存它,并在识别用户后更新它。

您可以这样做:

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

const unsigned *tokenBytes = [deviceToken bytes];
NSString *hexToken = [NSString stringWithFormat:@"%08x%08x%08x%08x%08x%08x%08x%08x",
                      ntohl(tokenBytes[0]), ntohl(tokenBytes[1]), ntohl(tokenBytes[2]),
                      ntohl(tokenBytes[3]), ntohl(tokenBytes[4]), ntohl(tokenBytes[5]),
                      ntohl(tokenBytes[6]), ntohl(tokenBytes[7])];
[[MyModel sharedModel] setApnsToken:hexToken];

}

这样您就可以将 apns 令牌保存在模型对象 (MyModel) 中。稍后,当您识别用户(通过登录/注册或其他方法)时,

您可以调用此方法

[self sendProvidedDeviceToken: [[MyModel sharedModel] apnsToken] forUserWithId: userId];  //Custom method

,这样您就将设备令牌与用户链接起来。希望这有帮助!

generally you don't update the apns token on server in the delegate method itself. you save it and update it later when you have the user identified.

You can do it this way:

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

const unsigned *tokenBytes = [deviceToken bytes];
NSString *hexToken = [NSString stringWithFormat:@"%08x%08x%08x%08x%08x%08x%08x%08x",
                      ntohl(tokenBytes[0]), ntohl(tokenBytes[1]), ntohl(tokenBytes[2]),
                      ntohl(tokenBytes[3]), ntohl(tokenBytes[4]), ntohl(tokenBytes[5]),
                      ntohl(tokenBytes[6]), ntohl(tokenBytes[7])];
[[MyModel sharedModel] setApnsToken:hexToken];

}

with this you saved the apns token in the Model object (MyModel). And later when you have your user identified (by login/sign up or whatever method)

you can call this method

[self sendProvidedDeviceToken: [[MyModel sharedModel] apnsToken] forUserWithId: userId];  //Custom method

This way you have linked the device token with user. Hope this helps!

尐籹人 2025-01-09 01:16:13

您在自定义方法中注册时需要发送设备名称。代码应如下所示。您可以发送适合您的上下文的任何信息,例如用户名(如果应用程序使用某种用户名)。由您决定将哪些信息发送到在令牌和设备之间建立连接的服务器。

// Delegation methods
- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)devToken {
    const void *devTokenBytes = [devToken bytes];
    self.registered = YES;
    [self sendProviderDeviceToken:devTokenBytes deviceName:[[UIDevice currentDevice] name]]; // custom method
}

You need to send the device name when registering in your custom method. The code should look something like below. You could send along any information that is appropriate for your context, like a username if the application uses some kind username. It is up to you which to decide which information to send to your server that makes a connection between the token and the device.

// Delegation methods
- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)devToken {
    const void *devTokenBytes = [devToken bytes];
    self.registered = YES;
    [self sendProviderDeviceToken:devTokenBytes deviceName:[[UIDevice currentDevice] name]]; // custom method
}
夜灵血窟げ 2025-01-09 01:16:13

您可以将所需的任何信息发送到您自己的推送服务。

但重要的一点是:推送令牌不是设备令牌 (UDID)。推送令牌对于每个请求它们的应用程序来说都是唯一的,并且可以而且确实会发生变化。如果您还想获取设备名称,您可以调用 [[UIDevice currentDevice] name],并将其发布到您用于存储推送令牌的任何设备上。

It's up to you to send whatever information you need to your own push service.

But an important point: the push token is not the device token (UDID). Push tokens are unique to each app that requests them, and can and do change. If you want to get the device name in addition to that, you can call [[UIDevice currentDevice] name], and post it off to whatever you are using to store your push tokens.

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