启用/禁用推送通知

时间:2014-09-26 19:59:21

标签: objective-c xcode

我尝试在用户单击按钮时启用/禁用推送通知。当用户选择禁用通知时,一切都是正确的,不会收到推送通知。但是当用户再次切换以启用远程通知时,则不会收到这些通知。 在这一刻,我检查了设置 - >通知,我看到该应用程序未注册接收推送通知。

这是我的按钮代码(禁用):

if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.000000)
            {
                UIUserNotificationSettings * settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeNone categories:nil];
                [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
                [[UIApplication sharedApplication] registerForRemoteNotifications];
            }

            else
            {
                //[[UIApplication sharedApplication] registerForRemoteNotificationTypes:UIUserNotificationTypeNone];
                [[UIApplication sharedApplication] registerForRemoteNotificationTypes:UIRemoteNotificationTypeNone];
                [[UIApplication sharedApplication] registerForRemoteNotifications];
            }

这是我的按钮代码(启用):

if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.000000)
            {
                UIUserNotificationType types = UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound;
                UIUserNotificationSettings * settings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
                [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
            }

            else
            {
                [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
            }

在AppDelegate中,didFinishLaunchingWithOptions,该应用程序已正确注册远程通知:

if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.000000)
{
    UIUserNotificationType types = UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound;
    UIUserNotificationSettings * settings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
    [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
    [[UIApplication sharedApplication] registerForRemoteNotifications];
}

else
{
    //[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)];
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
    [[UIApplication sharedApplication] registerForRemoteNotifications];
}

谢谢

0 个答案:

没有答案