Ios 启用和禁用远程通知

Ios 启用和禁用远程通知,ios,objective-c,xcode,Ios,Objective C,Xcode,应用程序的用户必须能够通过按开/关按钮启用/禁用远程通知 应用程序在didFinishLaunchingWithOptions中注册远程通知。 当用户单击按钮禁用远程通知时,代码为 // Register for Push Notifications, if running iOS 8 if ([application respondsToSelector:@selector(registerUserNotificationSettings:)])

应用程序的用户必须能够通过按开/关按钮启用/禁用远程通知

应用程序在didFinishLaunchingWithOptions中注册远程通知。 当用户单击按钮禁用远程通知时,代码为

// Register for Push Notifications, if running iOS 8
                    if ([application respondsToSelector:@selector(registerUserNotificationSettings:)])
                    {
                        UIUserNotificationType userNotificationTypes = UIUserNotificationTypeNone;
                        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:userNotificationTypes categories:nil];
                        [application registerUserNotificationSettings:settings];
                        [application registerForRemoteNotifications];
                    }

                    else
                    {
                        // Register for Push Notifications before iOS 8
                        [application registerForRemoteNotificationTypes:UIRemoteNotificationTypeNone];
                    }
这很有效。远程通知已禁用

但当用户再次单击以启用远程通知时

    if ([application respondsToSelector:@selector(registerUserNotificationSettings:)])
                    {
                        UIUserNotificationType userNotificationTypes = (UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound);
                        UIUserNotificationSettings * settingsAvailable = [UIUserNotificationSettings settingsForTypes:userNotificationTypes categories:nil];
                        [application registerUserNotificationSettings:settingsAvailable];
                        [application registerForRemoteNotifications];

                    }

                    else
                    {
                        // Register for Push Notifications before iOS 8
                        [application registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound)];

                    }
应用程序未注册任何类型的警报、徽章或声音


有什么建议吗?提前感谢。

您应该在这里分离出两个概念:第一个是请求用户允许接收推送通知,这就是您正在做的。一旦你得到了许可,就不要关掉它。我的理解是,您只需请求一次此权限,之后,必须在设置应用程序设置->通知中进行更改


您可能应该做的是请求启用推送通知一次,同时在应用程序中跟踪推送的生成位置以及用户是否希望接收推送。权限级别由iOS管理。辅助开/关级别由您管理

我试着这么做。每次按钮打开或关闭时,我都会将此选项存储在userDefault中,并尝试在AppDelegate didReceivePushNotifications中允许/不接收推送通知,但我不知道为什么远程通知仍然是通过电话获取的。您所做的一切都没有意义。当调用DidReceiveEmoteNotification时已经太晚了——推送已经发送、接收,用户可能已经看到了。正如我所说,您应该自己跟踪用户的偏好,并根据该偏好发送或不发送推送通知。可能您是对的,并且应该是实现这一点的最佳方式。非常感谢。