推送通知中的设备令牌

问题描述:

我只想向特定用户发送推送通知。推送通知中的设备令牌

从我在苹果文档中经历的。 用于推送通知注册的代码是这个

- (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字节创建并发送至server..but我将如何识别哪个设备令牌属于哪个用户的方法。所以如果我的设备名称是Shubhank的iPhone。我怎样才能发送我的iPhone是这个信息,这是我的设备令牌。

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

已经链接与用户的设备令牌这种方式。希望这可以帮助!

+0

非常感谢,做解决我在找什么......但我现在有重要的问题..每次应用程序启动时,更改标记字节更改.. – Shubhank 2012-02-04 15:23:45

+0

尽管并非总是如此,令牌将更改,但它是始终建议您每次应用程序启动时更新该令牌。 请注意/接受答案,如果它帮助你。 – 2012-02-04 15:25:39

+0

我会...但我想知道。这是现在令我困惑..可以说设备令牌是相同的..然后设备将重新注册在我的服务器..所以我必须开发一个机制,覆盖的作品? ..或者如果令牌更改..那么我会对旧设备名称做什么? – Shubhank 2012-02-04 15:29:16

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

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

+0

让我说我想让用户为他的设备输入一个特定的名字,所以我提出了一个他写ABC的文本字段。但我相信appdidRegisterForRemoteNotif已经被调用,直到这发生..所以我可以再次注册推送通知? – Shubhank 2012-02-04 15:14:09

+0

否 - 您需要挂上推送令牌,然后在收到设备名称后,再将设备名称发送到后端服务。 – lxt 2012-02-04 15:16:22

+0

[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)]; 这是应用程序启动时调用..所以我相信idRegisterForRemoteNotificati也将被调用之前,我可以显示我的用户的文本字段....所以我相信你说我可以稍后调用[自sendProvider:Userinput] ..但服务器将如何知道这个名字是为这个devTokenBytes – Shubhank 2012-02-04 15:21:28

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

// 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 
} 
+0

可以说我想让用户输入他的设备的特定名称,所以我提出了一个文本字段,他在哪里写ABC。但我相信appdidRegisterForRemoteNotif已经被调用,直到这发生..所以我可以再次注册推送通知? – Shubhank 2012-02-04 15:14:20

+2

最好在注册设备之前等待用户输入名称。在我目前的应用程序中,我等待注册,直到用户登录。没有什么要求您在启动时注册。 – 2012-02-04 15:16:21

+0

[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)]; 这是应用程序启动时调用..所以我相信idRegisterForRemoteNotificati也将被调用之前,我可以显示我的用户的文本字段....所以我相信你说我可以稍后调用[自sendProvider:Userinput] ..但是服务器将如何知道这个名字是来自这个devTokenBytes – Shubhank 2012-02-04 15:21:09