jbono
jbono

Reputation: 105

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!

Upvotes: 3

Views: 3574

Answers (2)

Shivam Tripathi
Shivam Tripathi

Reputation: 1493

Notifications could be disabled/enabled from device itself like this :

    func switchChanged(sender: UISwitch!) {
    print("Switch value is \(sender.isOn)")

    if(sender.isOn){
        UIApplication.shared.registerForRemoteNotifications()
    }
    else{
        UIApplication.shared.unregisterForRemoteNotifications()
    }
}

Upvotes: 10

Akash Singh Sisodia
Akash Singh Sisodia

Reputation: 136

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

Upvotes: 1

Related Questions