Device Token in Push Notification

I want to send a push notification only to specific users.

From what I looked at in Apple docs. The registration code for push notification is

- (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 appdidRegisterForRemoteNotif method .. I only see devToken bytes created and sent to the server ... but how can I determine which device token belongs to that user. Therefore, if my device name is Shubhank iPhone. How can I send information that my iPhone is and this is my device token.

+4
source share
3 answers

usually you do not update the apns token on the server in the e-mail itself. you save it and update it later when you define the user.

You can do it as follows:

 - (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 defined your user (by login / registration or any other method)

you can call this method

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

Thus, you have connected the device token with the user. Hope this helps!

+6
source

You need to send the device name when registering in your custom method. The code should look something like this. You can send any information that is appropriate for your context, for example, username, if the application uses some username. You decide what information to send to the server, which establishes 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 } 
+2
source

It is for you to send any information needed for your own push service.

But an important point: the token-token is not a device token (UDID). Access points are unique to each application that requests them, and can and can change. If you want to get the device name in addition to this, you can call [[UIDevice currentDevice] name] and send it to everything you use to store tokens.

+1
source

Source: https://habr.com/ru/post/1394766/


All Articles