xcode 如何检查 iPhone 上是否启用了通知

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9005889/
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-09-14 23:06:12  来源:igfitidea点击:

How to check that notifications are enabled on iPhone

iphonexcodepush-notificationapple-push-notifications

提问by Oksana

How to check that notification is enabled on iPhone? I install the application, application say to confirm enable push notification for application, i click ok. But if notification disabled on iPhone, this action does not enable notifications. How to check that?

如何检查 iPhone 上是否启用了通知?我安装了应用程序,应用程序说要确认为应用程序启用推送通知,我点击确定。但是如果在 iPhone 上禁用通知,则此操作不会启用通知。如何检查?

回答by Zar

This should work:

这应该有效:

UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
if (types == UIRemoteNotificationTypeNone) 
   // Disabled

回答by Shaheen Ghiassy

This changed in iOS8. To support both iOS8 and lower do

这在 iOS8 中发生了变化。支持iOS8及更低版本

+ (BOOL)notificationServicesEnabled {
    BOOL isEnabled = NO;

    if ([[UIApplication sharedApplication] respondsToSelector:@selector(currentUserNotificationSettings)]){
        UIUserNotificationSettings *notificationSettings = [[UIApplication sharedApplication] currentUserNotificationSettings];

        if (!notificationSettings || (notificationSettings.types == UIUserNotificationTypeNone)) {
            isEnabled = NO;
        } else {
            isEnabled = YES;
        }
    } else {
        UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
        if (types & UIRemoteNotificationTypeAlert) {
            isEnabled = YES;
        } else{
            isEnabled = NO;
        }
    }

    return isEnabled;
}