Activer ou désactiver les Notifications Push Iphone dans l'application

J'ai une application iphone qui permet de recevoir des notifications push. Actuellement je peux désactiver les notifications push pour mon application en allant dans les paramètres de l'iphone/Notifications.

Mais je veux ajouter un commutateur ou un bouton dans mon application pour activer ou désactiver les notifications push.

Cela peut être fait parce que je l'ai vu dans l'application iPhone foursqure l'a fait. Ils ont une section dans Paramètres Paramètres de notification d'appel et l'utilisateur peut activer ou désactiver différents types de notification pour le App.

Je regarde partout sur le net pour trouver une solution appropriée pour cela, mais toujours pas trouvé de moyen. Quelqu'un peut-il donner une idée de comment faire cela?

Merci d'avance :)

23
demandé sur Krunal 2012-05-09 09:59:37

3 réponses

La Première chose, c'est que vous can not enable and disable push notification dans l'intérieur de l'application. Si vous avez trouvé des applications qui l'ont fait, il doit y avoir une solution de contournement.

Comme si vous voulez faire à l'intérieur de l'application, utilisez un identifiant et envoyez-le au serveur en fonction du bouton Activer et désactiver la notification push. Donc, votre codage côté serveur utilise cet identifiant et fonctionne en fonction de cela. Comme identifiant est dire qu'il est activé que votre serveur enverra une notification sinon pas.

Vous pouvez vérifier ce jeu d'utilisateurs enable ou disable Push Notifications en utilisant le code suivant.

Activer ou désactiver les Notifications Push Iphone

UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
if (types == UIRemoteNotificationTypeNone) 
 // Yes it is..

J'espère que cela vous aidera..

20
répondu Nit 2012-05-09 06:10:08

[FYI-peu d'utilisateurs ont signalé qu'il a cessé de travailler sur iOS 10]

Vous pouvez facilement activer et désactiver les notifications push dans votre application en appelant respectivement registerForRemoteNotificationTypes et unregisterForRemoteNotificationTypes. J'ai essayé et ça marche.

31
répondu Varun Bhatia 2017-01-13 03:32:59

De manière pragmatique, il est possible d'activer et de désactiver la notification push en enregistrant et en désinscrivant la notification push.

Activer Les Notifications Push:

if #available(iOS 10.0, *) {
   // For iOS 10.0 +
   let center  = UNUserNotificationCenter.current()
   center.delegate = self
   center.requestAuthorization(options: [.sound, .alert, .badge]) { (granted, error) in
        if error == nil{
           DispatchQueue.main.async(execute: {
                 UIApplication.shared.registerForRemoteNotifications()
           }) 
        }
   }
}else{
    // Below iOS 10.0

    let settings = UIUserNotificationSettings(types: [.sound, .alert, .badge], categories: nil)
    UIApplication.shared.registerUserNotificationSettings(settings)

    //or
    //UIApplication.shared.registerForRemoteNotifications()
}

Délégué méthodes

@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {

}

@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {

}


func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    // .. Receipt of device token
}


func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
    // handle error
}

Désactiver Les Notifications Push:

UIApplication.shared.unregisterForRemoteNotifications()
1
répondu Krunal 2018-01-18 09:35:48