2016-11-10 1 views

Antwort

8

Diese Antwort ist veraltet und unterstützt nicht auf iOS 10, können Sie this beantworten.


Verwenden Sie diesen Code

let isRegisteredForRemoteNotifications = UIApplication.shared.isRegisteredForRemoteNotifications 
if isRegisteredForRemoteNotifications { 
    // User is registered for notification 
} else { 
    // Show alert user is not registered for notification 
} 
+0

Dies scheint nicht für iOS 10 zu funktionieren. Im Simulator habe ich auf "Do not Allow" geklickt und dieser Code sagte immer noch, dass Benutzer für Remote-Benachrichtigungen registriert ist. – tylerSF

+0

Funktioniert für iOS 10. Versuchen Sie es mit einem tatsächlichen Gerät anstelle des Simulators. –

+0

Es zeigt Ihnen nur, wenn das Token jemals generiert wurde (das Gerät wurde registriert), nicht, wenn die Benachrichtigungen blockiert wurden. – KlimczakM

26

habe ich versucht, Rajat-Lösung, aber es hat nicht funktioniert für mich auf iOS 10 (Swift 3). Es wurde immer gesagt, dass Push-Benachrichtigungen aktiviert wurden. Im Folgenden habe ich das Problem gelöst. Dies bedeutet "nicht aktiviert", wenn der Benutzer auf "Nicht zulassen" geklickt hat oder wenn Sie den Benutzer noch nicht gefragt haben.

let notificationType = UIApplication.shared.currentUserNotificationSettings!.types 
    if notificationType == [] { 
     print("notifications are NOT enabled") 
    } else { 
     print("notifications are enabled") 
    } 

PS: Die Methode currentUserNotificationSettings wurde in iOS 10.0 veraltet, aber es funktioniert immer noch.

+0

Funktioniert das auf iOS 9,8,7, etc ... oder brauche ich separaten Code? –

+0

Ich bin mir nicht sicher, ich habe es nur auf iOS 10 überprüft. – tylerSF

+3

Cam, ich habe gerade diesen Code auf 10.2 (auf einem Telefon) und auf 9.3 (auf dem Simulator) getestet und es funktionierte auf beiden. tylerSF, danke für die Lösung. – KeithB

31

Apple empfiehlt die Verwendung von UserNotifications Framework anstelle von gemeinsam genutzten Instanzen. Vergessen Sie also nicht, das UserNotifications Framework zu importieren. Da dieser Rahmen in iOS 10 neu ist es wirklich nur sicher für iOS10 diesen Code in Anwendungen Gebäude zu verwenden +

let current = UNUserNotificationCenter.current() 

current.getNotificationSettings(completionHandler: { (settings) in 
    if settings.authorizationStatus == .notDetermined { 
     // Notification permission has not been asked yet, go for it! 
    } 

    if settings.authorizationStatus == .denied { 
     // Notification permission was previously denied, go to settings & privacy to re-enable 
    } 

    if settings.authorizationStatus == .authorized { 
     // Notification permission was already granted 
    } 
}) 

Sie offizielle Dokumentation für weitere Informationen überprüfen können: https://developer.apple.com/documentation/usernotifications

+1

mir scheint dies ab Juli die richtige Antwort ist 2017. –

+1

warum nicht diese 'if' ist' if else' und 'if else'? –

+0

@ JeremyBader, es ist richtig. –

2

@ Rajat Antwort ist nicht genug.

  • isRegisteredForRemoteNotifications ist, dass Ihre App APNS und Gerät Token verbunden hat bekommen, kann dies ohne diese
  • currentUserNotificationSettings ist für Benutzerberechtigungen für stille Push-Benachrichtigung ist, gibt es keine Warnung, Banner oder Ton-Push-Benachrichtigung geliefert zu der App ist

Hier Check

static var isPushNotificationEnabled: Bool { 
    guard let settings = UIApplication.shared.currentUserNotificationSettings 
    else { 
     return false 
    } 

    return UIApplication.shared.isRegisteredForRemoteNotifications 
    && !settings.types.isEmpty 
} 
10

wenn Ihre App iOS 10 und iOS 8 unterstützt, 9 Verwendung unter Code

//at the top declare UserNotifications 
//to use UNUserNotificationCenter 
import UserNotifications 

Dann

if #available(iOS 10.0, *) { 
     let current = UNUserNotificationCenter.current() 
     current.getNotificationSettings(completionHandler: { (settings) in 
      if settings.authorizationStatus == .notDetermined { 
       // Means you can request 
      } 

      if settings.authorizationStatus == .denied { 
       // User should enable notifications from settings & privacy 
       // show an alert or sth 
      } 

      if settings.authorizationStatus == .authorized { 
       // It's okay, no need to request 
      } 
     }) 
    } else { 
     // Fallback on earlier versions 
     if UIApplication.shared.isRegisteredForRemoteNotifications { 
      print("APNS-YES") 
     } else { 
      print("APNS-NO") 
     } 
    } 
3

in iOS11, Swift 4 ...

UNUserNotificationCenter.current().getNotificationSettings { (settings) in 
     if settings.authorizationStatus == .authorized { 
      // Already authorized 
     } 
     else { 
      // Either denied or notDetermined 
      UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { 
       (granted, error) in 
        // add your own 
       UNUserNotificationCenter.current().delegate = self 
       let alertController = UIAlertController(title: "Notification Alert", message: "please enable notifications", preferredStyle: .alert) 
       let settingsAction = UIAlertAction(title: "Settings", style: .default) { (_) -> Void in 
        guard let settingsUrl = URL(string: UIApplicationOpenSettingsURLString) else { 
         return 
        } 
        if UIApplication.shared.canOpenURL(settingsUrl) { 
         UIApplication.shared.open(settingsUrl, completionHandler: { (success) in 
         }) 
        } 
       } 
       let cancelAction = UIAlertAction(title: "Cancel", style: .default, handler: nil) 
       alertController.addAction(cancelAction) 
       alertController.addAction(settingsAction) 
       DispatchQueue.main.async { 
        self.window?.rootViewController?.present(alertController, animated: true, completion: nil) 

       } 
      } 
     } 
    } 
2

Hier ist eine Lösung für eine Zeichenfolge immer die aktuelle Berechtigung beschreibt, die mit iOS 9 Trog arbeitet iOS 11 , mit Swift 4. Diese Implementierung verwendet When für Versprechen.

import UserNotifications 

private static func getNotificationPermissionString() -> Promise<String> { 
    let promise = Promise<String>() 

    if #available(iOS 10.0, *) { 
     let notificationCenter = UNUserNotificationCenter.current() 
     notificationCenter.getNotificationSettings { (settings) in 
      switch settings.authorizationStatus { 
      case .notDetermined: promise.resolve("not_determined") 
      case .denied: promise.resolve("denied") 
      case .authorized: promise.resolve("authorized") 
      } 
     } 
    } else { 
     let status = UIApplication.shared.isRegisteredForRemoteNotifications ? "authorized" : "not_determined" 
     promise.resolve(status) 
    } 

    return promise 
} 
Verwandte Themen