我正在寻找一种方法来确定用户是否有,通过设置,启用或禁用他们的推送通知我的应用程序。


当前回答

完全容易复制和粘贴代码构建从@ZacBowling的解决方案(https://stackoverflow.com/a/1535427/2298002)

这也将把用户带到你的应用程序设置,并允许他们立即启用

我还添加了一个解决方案,用于检查位置服务是否启用(以及带来的设置)

// check if notification service is enabled
+ (void)checkNotificationServicesEnabled
{
    if (![[UIApplication sharedApplication] isRegisteredForRemoteNotifications])
    {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Notification Services Disabled!"
                                                            message:@"Yo don't mess around bro! Enabling your Notifications allows you to receive important updates"
                                                           delegate:self
                                                  cancelButtonTitle:@"Cancel"
                                                  otherButtonTitles:@"Settings", nil];

        alertView.tag = 300;

        [alertView show];

        return;
    }
}

// check if location service is enabled (ref: https://stackoverflow.com/a/35982887/2298002)
+ (void)checkLocationServicesEnabled
{
    //Checking authorization status
    if (![CLLocationManager locationServicesEnabled] || [CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied)
    {

        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Location Services Disabled!"
                                                            message:@"You need to enable your GPS location right now!!"
                                                           delegate:self
                                                  cancelButtonTitle:@"Cancel"
                                                  otherButtonTitles:@"Settings", nil];

        //TODO if user has not given permission to device
        if (![CLLocationManager locationServicesEnabled])
        {
            alertView.tag = 100;
        }
        //TODO if user has not given permission to particular app
        else
        {
            alertView.tag = 200;
        }

        [alertView show];

        return;
    }
}

// handle bringing user to settings for each
+ (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{

    if(buttonIndex == 0)// Cancel button pressed
    {
        //TODO for cancel
    }
    else if(buttonIndex == 1)// Settings button pressed.
    {
        if (alertView.tag == 100)
        {
            //This will open ios devices location settings
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"prefs:root=LOCATION_SERVICES"]];
        }
        else if (alertView.tag == 200)
        {
            //This will open particular app location settings
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
        }
        else if (alertView.tag == 300)
        {
            //This will open particular app location settings
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
        }
    }
}

GLHF !

其他回答

为了完成这个答案,它可以这样工作……

UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
switch (types) {
   case UIRemoteNotificationTypeAlert:
   case UIRemoteNotificationTypeBadge:
       // For enabled code
       break;
   case UIRemoteNotificationTypeSound:
   case UIRemoteNotificationTypeNone:
   default:
       // For disabled code
       break;
}

编辑:这是不对的。因为这些是位智慧的东西,它不会与开关工作,所以我结束使用这个:

UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
UIRemoteNotificationType typesset = (UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge);
if((types & typesset) == typesset)
{
    CeldaSwitch.chkSwitch.on = true;
}
else
{
    CeldaSwitch.chkSwitch.on = false;
}

iOS8+ (OBJECTIVE C)

#import <UserNotifications/UserNotifications.h>


[[UNUserNotificationCenter currentNotificationCenter]getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {

    switch (settings.authorizationStatus) {
          case UNAuthorizationStatusNotDetermined:{

            break;
        }
        case UNAuthorizationStatusDenied:{

            break;
        }
        case UNAuthorizationStatusAuthorized:{

            break;
        }
        default:
            break;
    }
}];

不幸的是,所提供的这些解决方案都没有真正解决问题,因为在提供相关信息时,api严重缺乏。你可以做一些猜测,但是使用currentUserNotificationSettings (iOS8+)在目前的形式下还不足以真正回答这个问题。虽然这里的许多解决方案似乎表明,要么是that,要么是isregisteredforremotenotification是一个更明确的答案,但实际上不是。

考虑一下:

with isregisteredforremotenotifations文档声明:

如果应用程序当前为远程通知注册,则返回YES,考虑到任何系统范围的设置…

However if you throw a simply NSLog into your app delegate to observe the behavior it is clear this does not behave the way we are anticipating it will work. It actually pertains directly to remote notifications having been activated for this app/device. Once activated for the first time this will always return YES. Even turning them off in settings (notifications) will still result in this returning YES this is because, as of iOS8, an app may register for remote notifications and even send to a device without the user having notifications enabled, they just may not do Alerts, Badges and Sound without the user turning that on. Silent notifications are a good example of something you may continue to do even with notifications turned off.

就currentUserNotificationSettings而言,它表示以下四件事之一:

警报开启 徽章都戴上了 声音开着 没有打开。

这完全没有给你任何关于其他因素或通知开关本身的指示。

事实上,用户可能会关闭徽章、声音和提醒,但仍然会在锁定屏幕或通知中心显示。该用户仍然可以收到推送通知,并且能够在锁定屏幕和通知中心看到它们。他们打开了通知开关。但是currentUserNotificationSettings会返回:UIUserNotificationTypeNone。这并不能真正反映用户的实际设置。

我们可以做一些猜测:

if isRegisteredForRemoteNotifications is NO then you can assume that this device has never successfully registered for remote notifications. after the first time of registering for remote notifications a callback to application:didRegisterUserNotificationSettings: is made containing user notification settings at this time since this is the first time a user has been registered the settings should indicate what the user selected in terms of the permission request. If the settings equate to anything other than: UIUserNotificationTypeNone then push permission was granted, otherwise it was declined. The reason for this is that from the moment you begin the remote registration process the user only has the ability to accept or decline, with the initial settings of an acceptance being the settings you setup during the registration process.

尽管Zac的答案在iOS 7之前是完全正确的,但自从iOS 8到来后,它就发生了变化。因为enabledRemoteNotificationTypes从iOS 8开始就已经弃用了。对于iOS 8和更高版本,你需要使用isregisteredforremotenotifizations。

—>使用enabledRemoteNotificationTypes >使用isregisteredforremotenotifizations。

re:

这是正确的

if (types & UIRemoteNotificationTypeAlert)

但是下面也是正确的!(因为UIRemoteNotificationTypeNone是0)

if (types == UIRemoteNotificationTypeNone) 

参见以下内容

NSLog(@"log:%d",0 & 0); ///false
NSLog(@"log:%d",1 & 1); ///true
NSLog(@"log:%d",1<<1 & 1<<1); ///true
NSLog(@"log:%d",1<<2 & 1<<2); ///true
NSLog(@"log:%d",(0 & 0) && YES); ///false
NSLog(@"log:%d",(1 & 1) && YES); ///true
NSLog(@"log:%d",(1<<1 & 1<<1) && YES); ///true
NSLog(@"log:%d",(1<<2 & 1<<2) && YES); ///true