IOS - 如何在注销时禁用推送通知? [英] IOS - How to disable push notification at logout?

查看:19
本文介绍了IOS - 如何在注销时禁用推送通知?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序在登录时在我的服务器中注册帐户以启用聊天的推送通知.但是我还没有实现注销时的帐户注销,所以此时如果我在同一设备中使用2个帐户登录它可以收到通知两个帐户.同时,我的通知中心有一个 POST 服务,它从接收通知中心取消注册登录名+设备令牌".我应该在哪里调用它?我是否必须使用 unregisterForRemoteNotifications?我只是想从推送通知中取消注册帐户+设备令牌,而不是永远禁用整个应用程序通知.

My app registers the account at login in my server to enable push notification for a chat. Yet I haven't implemented yet the unregistration of the account at logout, so in this moment if I do the login with 2 accounts in the same device it can take the notification of both the accounts. At the same time, my notification center has a POST service which unregisters the 'login_name+ device token' from receive notification center. Where should I call it? Do I have to use unregisterForRemoteNotifications? I just want to unregister the account+Device token from push notification, not to disable the entire app notification forever.

我可以在 didRegisterForRemoteNotificationsWithDeviceToken 函数上保存我的设备令牌,比如

Can I save my device token on didRegisterForRemoteNotificationsWithDeviceToken function like

 $ [[NSUserDefaults standardUserDefaults] setObject:hexToken forKey:DEVICE_KEY];

然后,在注销时,像

  NSString *deviceToken = [userDefaults objectForKey:DEVICE_KEY];
    if(deviceToken != NULL){
       [self.engine removeDeviceToken:deviceToken];
     }

推荐答案

我不确定我是否理解正确,但是如果您不想为应用禁用推送通知,那么您不应该调用取消注册远程通知.您可以做的是,当用户点击注销按钮时,您可以向您的服务器发出注销请求,然后服务器从该帐户中删除notificationID,注销请求完成后,您只需在本地执行注销(更新UI等).

I'm not sure if i got it correctly, but if you don't want to disable push notifications for the app, then you should't call the unregisterForRemoteNotifications. What you can do is, when the user taps the logout button, you can make a logout request to your server, which then removes the notificationID from that account, and after the logout request is completed, you just perform the logout locally (update UI etc).

有关评论的更多信息:

是的,首先,您应该在每次启动时调用 registerForRemoteNotificationTypes 方法,因为设备令牌可以更改.委托方法之后

Yes, first of all, you should call registerForRemoteNotificationTypes method at every launch, because device token can change. After the delegate method

- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)devToken

被调用,可以获取设备令牌并保存到NSUserDefault.这样,当用户登录时,您可以获得最新的设备令牌(如果更改),并将其发送到您的服务器以添加到该帐户.

is called, you can get the device token and save it to NSUserDefault. That way when the user logs in, you can get the up-to-date device token (if changed), and send it to your server to be added to that account.

所以代码可能看起来像这样

So the code might look like this

- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)devToken {
    NSString *newToken = [devToken description];
    newToken = [newToken stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];
    newToken = [newToken stringByReplacingOccurrencesOfString:@" " withString:@""];
    NSString *notificationID = [newToken description];

    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    [prefs setObject:notificationID forKey:@"notification_id"];
    [prefs synchronize];
}

所以,现在当用户登录时,只需获取通知 ID 并将其发送到您的服务器

So, now when the user logs in, just get the notification ID and send it to your server

- (IBAction)userTappedLoginButton {
    // Make your login request
    // You can add the notification id as a parameter
    // depending on your web service, or maybe make
    // another request just to update notificationID
    // for a member

    NSString *notificationID = [[NSUserDefaults standardUserDefaults] objectForKey:@"notification_id"];
    ...
    ...
}

这篇关于IOS - 如何在注销时禁用推送通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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