4

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

根据我在 Apple 文档中的经历。注册推送通知的代码是这样的

- (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 是这个并且这是我的设备令牌的信息。

4

3 回答 3

6

通常,您不会在委托方法本身中更新服务器上的 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

通过这种方式,您已将设备令牌与用户相关联。希望这可以帮助!

于 2012-02-04T15:19:44.877 回答
2

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

// 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
}
于 2012-02-04T15:07:02.770 回答
1

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

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

于 2012-02-04T15:03:23.970 回答