2017-10-10 18 views
3

Dans mon application, je souhaite être en mesure de vérifier si l'utilisateur a activé ou non les notifications. Dans iOS 10, je l'ai fait en utilisant une vérification dans le délégué.Vérifier si les notifications utilisateur sont activées après la dépréciation UILocalNotification

Cette vérification est maintenant dépréciée et je veux le mettre à jour, mais je ne peux pas comprendre ce qu'il faut utiliser dans iOS 11.

L'avertissement deprecation est la suivante:

currentUserNotificationSettings' a été dépréciée dans iOS 10.0: Utiliser UserNotifications du cadre - [UNUserNotificationCenter getNotificationSettingsWithCompletionHandler:] et - [UNUserNotificationCenter getNotificationCategoriesWithCompletionHandler:]

J'ai essayé de mettre à jour le code à l'aide de cet avertissement mais je n'arrive pas à le comprendre.

Si quelqu'un peut suggérer de toute façon d'obtenir un tel contrôle, cela aiderait beaucoup. Le code que j'ai utilisé pour iOS 10 est ci-dessous, merci.

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

Répondre

8

Étape 1: import UserNotifications

Étape 2:

UNUserNotificationCenter.current().getNotificationSettings { (settings) in 
    if settings.authorizationStatus == .authorized { 
    // Notifications are allowed 
    } 
    else { 
    // Either denied or notDetermined 
    } 
} 

Inspect les paramètres objet pour plus d'informations.

+0

comment gérez-vous cela dans iOS avant 10? – Josh

+0

Vous devez obtenir un objet 'UIUserNotificationSettings' à partir de ** UIApplication.shared ** via la méthode' currentUserNotificationSettings'. – McNight

+0

un grand merci :) – Josh

3

Première étape: - Vous devez ajouter le fichier d'en-tête comme

import UserNotifications 

Je l'ai utilisé checkpushNotification méthode pour vérifier si la notification utilisateur permettent ou non. Les utilisations appelaient cette méthode à partir de didFinishLaunchingWithOptions de la classe AppDelegate. J'espère que cela vous aidera, si un problème, puis commenter ci-dessous.

Dernière étape: -

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") 
       } 
      } 
     } else { 

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

       print("enabled notification setting") 
      }else{ 

       print("setting has been disabled") 
      } 
     } 
    } 
+0

et pour les versions antérieures? – Josh

+0

@Josh j'ai mis à jour pour la version précédente ci-dessus. Si cela n'a pas fonctionné, veuillez commenter ci-dessous. –

+0

@ Amrit Tiwari implique-t-il que si isNotificationEnabled = false, cela a été refusé? fonctionne-t-il de la même manière que dans le cas précédent de commutateur dans votre exemple? – Josh