Comment activer / désactiver la notification push de l'application?

Dans mon application je veux activer/désactiver la notification push à partir de la page des paramètres de mon application elle-même.Quelqu'un peut-il me suggérer une solution pour activer/désactiver l'état de l'application dans le centre de notifications de l'application ?

22
demandé sur Krunal 2012-12-11 10:12:46

2 réponses

Vous pouvez enregistrer et annuler l'enregistrement de la notification à distance avec le code ci-dessous.

Enregistrez la RemoteNotification avec le code ci-dessous..signifie activer la notification

//-- Set Notification
if ([[UIApplication sharedApplication]respondsToSelector:@selector(isRegisteredForRemoteNotifications)])
{
    // For iOS 8 and above
    [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];

    [[UIApplication sharedApplication] registerForRemoteNotifications];
}
else
{
    // For iOS < 8
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:
     (UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)];
}

Et désactivez-le avec le code ci-dessous.

[[UIApplication sharedApplication] unregisterForRemoteNotifications];
56
répondu Paras Joshi 2016-01-26 09:59:14

Swift 4

Activer la Notification Push (configuration de l'application):

if #available(iOS 10.0, *) {

   // SETUP FOR NOTIFICATION 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 {

   // SETUP FOR NOTIFICATION FOR iOS < 10.0

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

   // This is an asynchronous method to retrieve a Device Token
   // Callbacks are in AppDelegate.swift
   // Success = didRegisterForRemoteNotificationsWithDeviceToken
   // Fail = didFailToRegisterForRemoteNotificationsWithError
    UIApplication.shared.registerForRemoteNotifications()
}

Déléguer des méthodes pour gérer les notifications push

@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) {
    // ...register device token with our Time Entry API server via REST
}


func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
    //print("DidFaildRegistration : Device token for push notifications: FAIL -- ")
    //print(error.localizedDescription)
}

Désactiver La Notification Push:

UIApplication.shared.unregisterForRemoteNotifications()
7
répondu Krunal 2018-01-05 11:35:51