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

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

问题描述

我的应用程序在注册登录时的帐户在我的服务器启用聊天推送通知,但如果我做的登录与2个帐户在我还没有在这一刻实现在注销账户的尚注销,所以同样的设备,它需要两个账户的通知。与此同时,我的通知中心拥有从注销接收通知中心LOGIN_NAME +设备令牌的POST服务泰德。我应该在哪里打电话吗?我已经使用unregisterForRemoteNotifications?我只是想从注销推送通知的户头+设备符号,而不是永远禁用整个应用程序的通知。

我可以节省didRegisterForRemoteNotificationsWithDeviceToken功能我的设备令牌像

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

,然后注销时,叫我POST功能removeDeviceToken像

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


解决方案

我不知道如果我没有得到它,但如果你不想禁用应用程序推送通知,那么你should't调用unregisterForRemoteNotifications。你可以做的是,当用户点击注销按钮,你可以做一个注销请求到服务器,然后从该帐户中删除notificationID和注销请求完成后,你只需进行登出本地(更新UI等等)。

有关评论更多信息:

是的,首先,你应该叫registerForRemoteNotificationTypes方法在每次启动,因为设备令牌可以改变的。委托方法后

   - (无效)应用:(*的UIApplication)应用didRegisterForRemoteNotificationsWithDeviceToken:(NSData的*)DEVTOKEN

被调用时,你可以得到装置凭证并保存到NSUserDefault。这样,当用户登录时,你可以得到向上的最新设备令牌(如果更改),并将其发送到你的服务器被添加到该帐户。

所以,code可能看起来像这样

   - (无效)应用:(*的UIApplication)应用didRegisterForRemoteNotificationsWithDeviceToken:(NSData的*){DEVTOKEN
    * NSString的newToken = [DEVTOKEN描述];
    newToken = [newToken stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@<>中];
    newToken = [newToken stringByReplacingOccurrencesOfString:@withString:@];
    * NSString的notificationID = [newToken描述];    * NSUserDefaults的preFS = [NSUserDefaults的standardUserDefaults]
    [preFS的setObject:notificationID forKey:@notification_id];
    [preFS同步]。
}

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

   - (IBAction为)userTappedLoginButton {
    //使您的登录请求
    //您可以添加通知ID作为参数
    //根据您的网络服务,或者使
    //另一个请求只是为了更新notificationID
    //为成员    * NSString的notificationID = [[NSUserDefaults的standardUserDefaults] objectForKey:@notification_id];
    ...
    ...
}

my application register the account at login in my server to enable push notification for a chat, but 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 take the notification of both the accounts. At the same time, my notification center has a POST service thet unregister the login_name+ device token from receive notification center. Where should i call it? have i to use unregisterForRemoteNotifications? i just want to unregister the accout+Device token from push notification, not to disable the entire app notification forever.

Can I save my device token on didRegisterForRemoteNotificationsWithDeviceToken function like

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

and then, at logout, call my POST function "removeDeviceToken" like

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

解决方案

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).

More info about comment:

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

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];
}

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天全站免登陆