启用&禁用远程通知

时间:2014-11-17 20:09:49

标签: ios objective-c xcode

app的用户必须能够通过按下开/关按钮来启用/禁用远程通知。

app在“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)];

                    }

该应用不会注册任何类型(提醒,徽章或声音)

一些建议?提前谢谢。

1 个答案:

答案 0 :(得分:0)

您应该在这里分出两个概念:首先是要求用户获得接收推送通知的权限,这就是您正在做的事情。获得该许可后,请勿将其关闭。我的理解是,您只需要获得此权限一次,之后,必须在设置应用内进行更改(设置 - >通知)。

您应该做的是要求启用推送通知一次,同时还要在应用中跟踪以及无论用户是否想要接收推送而生成推送的位置。 权限级别由iOS管理。辅助(开/关)级别由您管理。