Associating a user account with a device token for push notifications

I have an application configured with an API server, and each user creates an account via email or facebook when the application is downloaded. This information is stored in the back. I want to enable push notifications so that they are user specific. I know what needs to be done on the back with an APNS server, etc. My question connects the device token with the user account, so I can send the correct user the correct information based on the logic from my servers.

I know that I put this code in applicationDidFinishLaunching :

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window.rootViewController = self.viewController; [self.window makeKeyAndVisible]; // Let the device know we want to receive push notifications [[UIApplication sharedApplication] registerForRemoteNotificationTypes: (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)]; return YES; } 

And then I take the device id from here:

 - (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken { NSLog(@"My token is: %@", deviceToken); } - (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error { NSLog(@"Failed to get token, error: %@", error); } 

I understand correctly that when a user signs up, can I just send the device token to my server and associate it with this user? I can just add an API call attribute to handle it, I just want to make sure that this is in line with expected Apple practices. Then, when the user logs out, I simply clear the device token from my username so that if another user logs on to the same device, I will not have a duplicated token.

+4
source share
1 answer

What you have to do. In didRegisterForRemoteNotificationsWithDeviceToken you can locally store the device token, and then when the user logs in, you can send the device token with the user ID to your server.

+4
source

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


All Articles