Search code examples
iosswiftfirebasefirebase-notifications

How to disconnect Firebase Notifications on iOS (Swift 3)?


I'm working on an App with Firebase Notifications. I have configured it in AppDelegate, and they're working good.

The question: I have a settings view with a switch, to turn on/off notifications, and I don't know how to disable notifications. I've tried this, but is not working:

@IBAction func changeSwitch(_ sender: Any) {
    if mySwitch.isOn {
        print("NOTIFICATIONS ON")

        connectToFcm()

    } else {
        print("NOTIFICATIONS OFF")

        FIRMessaging.messaging().disconnect()
    }
}

func connectToFcm() {
    FIRMessaging.messaging().connect { (error) in
        if (error != nil) {
            print("Unable to connect with FCM. \(error)")
        } else {
            print("Connected to FCM.")
        }
    }
}

Maybe you can help me.

Thanks!


Solution

  • The easiest and fast way is to remove the device token from your backend database on turning off the notification