Device token in push notification

余生长醉 提交于 2019-12-05 16:51:24

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!

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
}

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!