Disable Push notification

У меня есть клас View Controller где пользователь решает включать ли уведомления в приложении.Когда он включает, ему каждый день приходят пуши. В другом. View controller я добавил switch который отвичает за вкл и выкл пушей. Но почему то при любом состоянии пуши всеровно приходят. ПОдскажите в чем проблема.

AppDelegate.swift

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

        completionHandler([.alert, .sound])
    }

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {


        UNUserNotificationCenter.current().delegate = self
        UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound])
        {
            (granted,error ) in
            print("granted\(granted)")
        }
        return true
    }

RegisterPush.swift

func setupNotification()
    {
        let content = UNMutableNotificationContent()
            content.title = NSLocalizedString("text",
                                              comment: "content.Title.Push")
            content.body =
            NSLocalizedString("text",
                              comment: "content.Body.Push")
            content.sound = UNNotificationSound.default

        let date = Date()
        var triggerDate = Calendar.current.dateComponents([.hour,.minute,.second,], from: date)
            triggerDate.hour = 9
            triggerDate.minute = 0
            triggerDate.second = 0

        let trigger = UNCalendarNotificationTrigger(dateMatching: triggerDate, repeats: true)
        let request = UNNotificationRequest(identifier: "requestId",
                                            content: content,
                                            trigger: trigger)

        UNUserNotificationCenter.current().add(request, withCompletionHandler: nil )

        defaults.set(true, forKey: notificationKey)
    }

Settings

@IBAction func switchAction(_ sender: UISwitch) {
        print(" switch IF ")
        if(sender.isOn){
            print("on")
         notification.setupNotification()
            defaults.set(sender.isOn, forKey: "notification")

        }  else{
            print("Off") // Тут почему то не отключаютяс пуши
            UIApplication.shared.unregisterForRemoteNotifications()
            defaults.set(sender.isOn, forKey: "notification")
        }

    }

Ответы (1 шт):

Автор решения: schmidt9

Вместо UIApplication.shared.unregisterForRemoteNotifications() используйте UNUserNotificationCenter.current().removeAllPendingNotificationRequests()

→ Ссылка