Shu*_*ank 4 iphone objective-c devicetoken ios
我想只向某些用户发送推送通知.
从我在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);
}
Run Code Online (Sandbox Code Playgroud)
在方法appdidRegisterForRemoteNotif..我只看到创建的devToken字节并发送到服务器..但我如何识别哪个设备令牌属于哪个用户.所以,如果我的设备名称是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];
}
Run Code Online (Sandbox Code Playgroud)
通过这个,你在模型对象(MyModel)中保存了apns标记.稍后当您确定用户时(通过登录/注册或任何方法)
你可以调用这个方法
[self sendProvidedDeviceToken: [[MyModel sharedModel] apnsToken] forUserWithId: userId]; //Custom method
Run Code Online (Sandbox Code Playgroud)
这样您就可以将设备令牌与用户链接起来.希望这可以帮助!
| 归档时间: |
|
| 查看次数: |
7313 次 |
| 最近记录: |