iphone - 推送通知中的设备 token

标签 iphone objective-c ios devicetoken

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

根据我在 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 字节..但是我将如何识别哪个设备 token 属于哪个用户。因此,如果我的设备名称是 Shubhank 的 iPhone。我如何发送我的 iPhone 是这个并且这是我的设备 token 的信息。

最佳答案

通常您不会在委托(delegate)方法本身中更新服务器上的 apns token 。您保存它并在识别用户后更新它。

你可以这样做:

- (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 token 保存在模型对象 (MyModel) 中。稍后当您确定您的用户时(通过登录/注册或任何方法)

你可以调用这个方法

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

这样您就可以将设备 token 与用户关联起来。希望这对您有所帮助!

关于iphone - 推送通知中的设备 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9141757/

相关文章:

ios - 如何在 ios swift 中安排周一、周二和周五下午 5 点的本地通知?

ios - 当我尝试将数据源分配给自定义 UIView 中的属性时,EXC_BAD_ACCESS 崩溃

ios - 在 ios 8 上使用 xib 抛出异常的自定义表格单元格 View

iphone - 增加touchesMoved的调用频率

ios - UISearchController 的 UISearchBar 在事件时移出屏幕

ios - 根据数据对数组进行排序

ios - 如何在 Objective-c ios 中抑制 PMD 重复?

ios - 是否可以在我的应用程序中嵌入 wps

iphone - 如何在其他场景中传输高分 - iOS8 [SWIFT]

iphone - iPhone/iPad 上的 CALayer 自动调整大小 : How?