Iphone iOS:推送通知并在登录时重新注册设备。

Iphone iOS:推送通知并在登录时重新注册设备。,iphone,ios,apple-push-notifications,Iphone,Ios,Apple Push Notifications,每次用户登录时,我都需要注册设备以进行推送通知 现在在我的AppDelete.m中,我有以下代码: - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { ... [[UIApplication sharedApplication] registerForRemoteNotificationTypes:UIRemoteNotific

每次用户登录时,我都需要注册设备以进行推送通知

现在在我的AppDelete.m中,我有以下代码:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
...
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert];
}

- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{

    NSString *token = [[deviceToken description] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];
    token = [[token componentsSeparatedByString:@" "] componentsJoinedByString:@""];

    [self.pushClient registerDeviceToken:token withUser:loggedInUserName onSuccess:^{
        NSLog(@"successful registration");
    } onFailure:^(NSError *error) {
        NSLog(@"error: %@", [error userInfo]);
    }];
}
-(BOOL)应用程序:(UIApplication*)应用程序使用选项完成启动:(NSDictionary*)启动选项
{
...
[[UIApplication sharedApplication]registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert];
}
-(无效)应用程序:(UIApplication*)应用程序DIdRegisterForRemotionTificationswithDeviceToken:(NSData*)deviceToken
{
NSString*标记=[[deviceToken description]stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@”“];
token=[[token componentsSeparatedByString:@”“]componentsJoinedByString:@”“];
[self.pushClient registerDeviceToken:token with User:loggedInUserName on成功:^{
NSLog(@“成功注册”);
}onFailure:^(n错误*错误){
NSLog(@“error:%@,[error userInfo]);
}];
}

问:每次用户登录时,我需要做什么才能在我的应用程序委托中调用上述方法?i、 e.应用程序正在运行。用户使用不同的用户名注销并重新登录,现在我需要使用该用户名注册设备,我该怎么办?

用户登录后,始终可以调用registerforremotentificationtypes方法

您需要坚持使用APNS令牌,这是针对您的情况的前进方向。 当你的应用程序出现在前台时,你将收到呼叫,呼叫
registerForRemoteNotificationTypes
。现在

  • 从NSUserDefaults中删除旧的APNS令牌(如果有的话)(或任何您喜欢的持久性)
  • 将APNS令牌保存到NSUserDefaults中
  • 并使用保存的令牌