Device token in push notification

吃可爱长大的小学妹 提交于 2019-12-22 08:10:14

问题


I want to send push notification to certain users only.

From what I have gone through in Apple docs. The code to register for push notification is this

- (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);
}

In the method appdidRegisterForRemoteNotif..I see only devToken bytes created and send to the server..but how will I identify which device token belongs to which user. So if my device name is Shubhank's iPhone. How can I send the information that my iPhone is this and this is my device token.


回答1:


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!




回答2:


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
}



回答3:


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.



来源:https://stackoverflow.com/questions/9141757/device-token-in-push-notification

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