swift 和 OC 判斷用戶是否允許接收推送通知


 

#pragma mark --------------- 判斷用戶是否允許接收通知    oc

- (BOOL)isUserNotificationEnable {

    BOOL isEnable = NO;

    if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0f) { // iOS版本 >=8.0 處理邏輯

        UIUserNotificationSettings *setting = [[UIApplication sharedApplication] currentUserNotificationSettings];

        isEnable = (UIUserNotificationTypeNone == setting.types) ? NO : YES;

    } else { // iOS版本 <8.0 處理邏輯

        UIRemoteNotificationType type = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];

        isEnable = (UIRemoteNotificationTypeNone == type) ? NO : YES;

    }

    return isEnable;

}

 

 

#pragma mark --------------- 判斷用戶是否允許接收通知  swift

    func checkPushNotification(){

        if #available(iOS 10.0, *) {

            UNUserNotificationCenter.current().getNotificationSettings(){ (setttings) in

 

                switch setttings.authorizationStatus{

                case .authorized:

                    print("enabled notification setting啟動")

                case .denied:

                    print("setting has been disabled禁用")

                case .notDetermined:

                    print("something vital went wrong here出了問題")

                case .provisional:

                    print("something vital went wrong here出了問題")

                @unknown default:

                    print("something vital went wrong here出了問題")

                }

            }

        } else {

 

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

            if isNotificationEnabled == true{

                print("enabled notification setting啟動通知設置")

            }else{

                print("setting has been disabled設置已禁用")

            }

        }

        

    }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM