IOS - 如何在注销时禁用推送通知?

Ale*_*ani 9 apple-push-notifications ios

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

我可以将设备令牌保存在didRegisterForRemoteNotificationsWithDeviceToken功能上吗

 $ [[NSUserDefaults standardUserDefaults] setObject:hexToken forKey:DEVICE_KEY];
Run Code Online (Sandbox Code Playgroud)

然后,在注销时,调用我的POST函数"removeDeviceToken"就好了

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

Dee*_*esh 24

您可以通过调用在应用程序中轻松启用和禁用推送通知

要注册,请致电: registerForRemoteNotificationTypes:

[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
     (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
Run Code Online (Sandbox Code Playgroud)

要取消注册,请致电: unregisterForRemoteNotificationTypes:

[[UIApplication sharedApplication] unregisterForRemoteNotifications];
Run Code Online (Sandbox Code Playgroud)

用于检查

启用或禁用iPhone推送通知尝试此代码

UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
if (types == UIRemoteNotificationTypeNone) 
 // Yes it is..
Run Code Online (Sandbox Code Playgroud)


mrt*_*mrt 8

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

有关评论的更多信息:

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

- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)devToken
Run Code Online (Sandbox Code Playgroud)

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

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

- (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];
}
Run Code Online (Sandbox Code Playgroud)

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

- (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"];
    ...
    ...
}
Run Code Online (Sandbox Code Playgroud)

  • 以上报价的网址:https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/IPhoneOSClientImp.html#//apple_ref/doc/uid/TP40008194-CH103-SW2 (3认同)
  • Apple文档说,"此外,永远不要缓存设备令牌并将其提供给您的提供商;总是在您需要时从系统获取令牌.如果您的应用程序先前已注册,则调用registerForRemoteNotificationTypes:导致操作系统传递设备令牌但是请注意,只要设备令牌发生变化,就可以调用委托方法,而不仅仅是响应您的应用注册或重新注册." (2认同)

小智 5

使用斯威夫特:

注册,

UIApplication.shared.registerForRemoteNotifications()
Run Code Online (Sandbox Code Playgroud)

要注销,

UIApplication.shared.unregisterForRemoteNotifications()
Run Code Online (Sandbox Code Playgroud)