将用户帐户链接到设备令牌以进行推送通知 [英] Linking user account to device token for push notifications

查看:21
本文介绍了将用户帐户链接到设备令牌以进行推送通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序设置了 API 服务器,每个用户在下载应用程序时通过电子邮件或 Facebook 创建一个帐户.这些信息都存储在后端.我想启用推送通知,以便它们是特定于用户的.我知道需要在后端使用 APNS 服务器等做什么.我的问题是将设备令牌与用户帐户相关联,以便我可以根据我的服务器的逻辑向正确的用户发送正确的信息.

I have an app set up with an API server and each user creates an account either by email or facebook when they download the app. That information is all stored in the back end. I want to enable push notifications so that they are user specific. I know what needs to be done on the back end with APNS server etc. My question is linking the Device Token with a user account so I can send the right user the right information based on logic from my servers.

我知道我把这段代码放在了applicationDidFinishLaunching中:

I know that I place this code in the 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;
}

然后我从这里获取设备 ID:

And then I grab 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);
}

我认为当用户登录时我可以将设备令牌发送到我的服务器并将其与该用户关联是否正确?我可以向 API 调用添加一个属性来处理这个问题,我只想确保这符合 Apple 的预期做法.然后,当用户退出时,我会从他们的用户名中清除设备令牌,这样如果另一个用户在同一台​​设备上登录,我就不会拥有重复的令牌.

Am I correct in thinking that when the user signs in I can just send the device token to my server and associate it with that user? I can just add an attribute to the API call to handle that, I just want to make sure that is inline with Apple's expected practices. Then when a user logs out I would just clear the Device Token from their username so that if another user logs in on the same device I wouldn't have a duplicate token.

推荐答案

这正是您应该做的.在 didRegisterForRemoteNotificationsWithDeviceToken 中,您可以将设备令牌存储在本地,然后,当用户登录时,您可以将带有用户标识符的设备令牌发送到您的服务器.

That's exactly what you should do. In didRegisterForRemoteNotificationsWithDeviceToken you can store the device token locally, and then, when the user logs-in, you can send the device token with the user identifier to your server.

这篇关于将用户帐户链接到设备令牌以进行推送通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆