ios 检测 iOS8 的“允许通知”是否打开/关闭

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25111644/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 01:41:51  来源:igfitidea点击:

detect "Allow Notifications" is on/off for iOS8

iosuilocalnotificationios8

提问by JosephT

I am trying to detect the Local notification settings for the App in iOS 8

我正在尝试检测 iOS 8 中应用程序的本地通知设置

for UIUserNotificationSettings, it returns me 7 as I have turned on all Badge, Sound & Alert.

因为UIUserNotificationSettings,它返回 7,因为我已打开所有徽章、声音和警报。

In the setting, I switch off "Allow Notification" , the app still return me 7 for UIUserNotificationSettings (Badge, Sound & Alert on). Is there a way to detect "Allow Notification" on/off?

在设置中,我关闭了 "Allow Notification" ,应用程序仍然返回 7 UIUserNotificationSettings (Badge, Sound & Alert on)。有没有办法检测“允许Notification”开/关?

- (void)application:(UIApplication *)application
    didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings{

    NSLog(@"---notificationSettings.types %d" , notificationSettings.types );
    if(notificationSettings.types!=7){
        UIAlertView * alert =[[UIAlertView alloc ] initWithTitle:@"Please turn on Notification"
                                                         message:@"Go to Settings > Notifications > App.\n Switch on Sound, Badge & Alert"
                                                        delegate:self
                                               cancelButtonTitle:@"Ok"
                                               otherButtonTitles: nil];
        [alert show];
    }
}

回答by Ponf

Method enabledRemoteNotificationTypesis deprecated since iOS8.

enabledRemoteNotificationTypes自 iOS8 起不推荐使用方法。

To check remote notifications status in iOS8 you can call

要检查 iOS8 中的远程通知状态,您可以调用

[[UIApplication sharedApplication] isRegisteredForRemoteNotifications];

it will return NO if user disable notifications in Settings. Documentationon isRegisteredForRemoteNotifications

如果用户在设置中禁用通知,它将返回 NO。文档isRegisteredForRemoteNotifications

Or you can retrieve all current notification settings:

或者您可以检索所有当前的通知设置:

[[UIApplication sharedApplication] currentUserNotificationSettings];

Documentationon currentUserNotificationSettings

文档currentUserNotificationSettings

回答by Adam Smaka

Swift 3+

斯威夫特 3+

let isRegisteredForLocalNotifications = UIApplication.shared.currentUserNotificationSettings?.types.contains(UIUserNotificationType.alert) ?? false

Swift 2.3

斯威夫特 2.3

let isRegisteredForLocalNotifications = UIApplication.sharedApplication().currentUserNotificationSettings()?.types.contains(UIUserNotificationType.Alert) ?? false

回答by mebeDB

I apologize for this answer/comment if it is in the wrong place. I am REALLY new at iOS programming and have never posted to stack overflow before. I think this should actually be a comment, but without a 50 rating I am not allowed. I also apologize if the explanation is somewhat rudimentary, but again, kind of new :).

如果这个答案/评论在错误的地方,我深表歉意。我是 iOS 编程的新手,以前从未发布过堆栈溢出。我认为这实际上应该是一个评论,但如果没有 50 分,我是不允许的。如果解释有些简陋,我也很抱歉,但同样,有点新:)。

I also wished to test for whether a user has changed what notifications are allowed/required by my app after the initial request. After trying to decipher the Apple documentation (the writers at Apple are either much smarter than I am, or the documentation is purposely obscure) and doing a bit of testing I tested for the value of

我还希望测试用户是否在初始请求后更改了我的应用程序允许/要求的通知。在尝试破译 Apple 文档(Apple 的作者要么比我聪明得多,要么文档故意模糊)并进行了一些测试后,我测试了

[[UIApplication sharedApplication] currentUserNotificationSettings].hash.

I believe that this returns a three bit hash value where bit one is for Banner notifications, bit 2 is for Sound notifications, and bit 3 is for Alert notifications.

我相信这会返回一个三位哈希值,其中第一个用于横幅通知,第 2 位用于声音通知,第 3 位用于警报通知。

So...

所以...

000 = 0 = no notifications.
001 = 1 = only banner,
010 = 2 = only sound,
011 = 3 = sound and banner, no alert
100 = 4 = only alert notifications
and so on until,
111 = 7 = all notifications on.

This also shows 0 if Allow Notificationsis turned off in the Settings app. Hope this helps.

如果在“设置”应用中关闭了“允许通知”,这也会显示 0 。希望这可以帮助。

回答by Manuel

To check the remote notifications status in iOS8 and iOS9 you call:

要检查 iOS8 和 iOS9 中的远程通知状态,请调用:

// Obj-C
[[UIApplication sharedApplication] isRegisteredForRemoteNotifications]; 
// Swift
UIApplication.sharedApplication().isRegisteredForRemoteNotifications

It returns trueif your app can receive remote notifications. However receivingremote notifications does not mean it will also displaythem to the user.

true如果您的应用程序可以接收远程通知,它会返回。然而,接收远程通知并不意味着它也会向用户显示它们。

The request registerForRemoteNotifications()succeeds in almost every case and the AppDelegate's didRegisterForRemoteNotificationsWithDeviceTokenis called which gives you the device token. The app can then receive silentremote notifications that are not displayed to the user. You can for example trigger background processes in the app when such a notification is received. See Documentation.

该请求registerForRemoteNotifications()几乎在所有情况下都会成功,并且didRegisterForRemoteNotificationsWithDeviceToken会调用AppDelegate并为您提供设备令牌。然后,该应用程序可以接收不向用户显示的静默远程通知。例如,您可以在收到此类通知时触发应用程序中的后台进程。请参阅文档

To not only receive but also displayremote notifications to the user you request permission with:

要不仅接收而且向用户显示远程通知,您请求权限:

// Swift
let notificatonSettings = UIUserNotificationSettings(forTypes: [.Badge, .Alert, .Sound], categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(notificatonSettings)

This will display a dialog to the user where they can allow or deny your request. Indifferent of their decision the app will still be able to receive remote notifications.

这将向用户显示一个对话框,他们可以在其中允许或拒绝您的请求。无论他们的决定如何,该应用程序仍将能够接收远程通知。

If the users allows, AppDelegate's didRegisterForRemoteNotificationsWithDeviceTokenis called.

如果用户允许,didRegisterForRemoteNotificationsWithDeviceToken则调用AppDelegate 。

To check whether the user allowed or denied your request or in fact changed the notifications permissions later in iOS setting you call:

要检查用户是否允许或拒绝了您的请求,或者实际上稍后在 iOS 设置中更改了通知权限,请调用:

// Obj-C
[[UIApplication sharedApplication] currentUserNotificationSettings];
// Swift
UIApplication.sharedApplication().currentUserNotificationSettings()   

See Documentation.

请参阅文档

回答by Denis Kanygin

This should work the very first time your app is launched user will get a dialog, to check if user denies or allows notifications, use:

这应该在您的应用程序第一次启动时起作用,用户将收到一个对话框,以检查用户是否拒绝或允许通知,请使用:

-(void)application:(UIApplication *)application didRegisterUserNotificationSettings:   (UIUserNotificationSettings *)notificationSettings
{
    if (notificationSettings.types) {
        NSLog(@"user allowed notifications");
        [[UIApplication sharedApplication] registerForRemoteNotifications];
    }else{
        NSLog(@"user did not allow notifications");
        // show alert here
    }
}

On consecutive launches, use:

在连续启动时,使用:

[[UIApplication sharedApplication] isRegisteredForRemoteNotifications];

回答by Sagar In

UIUserNotificationType notificationType = [[[UIApplication sharedApplication] currentUserNotificationSettings] types];

if(notificationType == UIRemoteNotificationTypeNone)
        {

            NSLog(@"OFF");
        }
        else{

            NSLog(@"ON");
        }

works for me

为我工作

回答by Marie Amida

Swift 2

斯威夫特 2

In AppDelegate:

在 AppDelegate 中:

func application(application: UIApplication, didRegisterUserNotificationSettings notificationSettings: UIUserNotificationSettings) {

    if (notificationSettings.types == .None){ // User did NOT allowed notifications


    }else{ // User did allowed notifications


    }

}

From any other ViewController:

从任何其他 ViewController:

    if UIApplication.sharedApplication().currentUserNotificationSettings()!.types.contains(.None){

    }else{

    }

回答by ACengiz

You can control the hash value of UIApplication.sharedApplication().currentUserNotificationSettings().

您可以控制 的哈希值UIApplication.sharedApplication().currentUserNotificationSettings()

if(UIApplication.instancesRespondToSelector(Selector("registerUserNotificationSettings:"))){
        if(UIApplication.sharedApplication().currentUserNotificationSettings().hashValue == 0){
            pushNotificationStatus = "false"
        } else {
            pushNotificationStatus = "true"
        }
}

回答by Irfan Gul

check this out. code is tried and tested.

看一下这个。代码经过尝试和测试。

- (BOOL)isUserNotificationAllowed {
    UIUserNotificationType types = [[UIApplication sharedApplication] currentUserNotificationSettings].types;
    if(types & UIUserNotificationTypeBadge || types & UIUserNotificationTypeSound || types & UIUserNotificationTypeAlert){
        return YES;
    }
    else {
        return NO;
    }
}

回答by AsTeR

Using .isRegisteredForRemoteNotifications()doesn't work (though it should). However, when the notifications are disabled or one of the type is not present, the following code does work.

使用.isRegisteredForRemoteNotifications()不起作用(尽管它应该)。但是,当通知被禁用或其中一种类型不存在时,以下代码确实有效。

func notificationsAreOk() -> Bool {
    let wishedTypes = UIUserNotificationType.Badge |
        UIUserNotificationType.Alert |
        UIUserNotificationType.Sound;
    let application = UIApplication.sharedApplication()
    let settings = application.currentUserNotificationSettings()
    if settings == nil {
        return false
    }
    if settings.types != wishedTypes {
        return false
    }
    return true
}

EDIT: after some tests is does not always work when notifications are disabled. I am considering to send a test notification to know when they work.

编辑:经过一些测试后,禁用通知时并不总是有效。我正在考虑发送测试通知以了解它们何时工作。