如何断开 iOS (Swift 3) 上的 Firebase 通知?

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

我正在开发一个带有 Firebase 通知的应用程序。我已经在 AppDelegate 中配置了它,它们运行良好。

问题:我有一个带有开关的设置视图,可以打开 on/off 通知,但我不知道如何禁用通知。我试过了,但没有用:

@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.")
        }
    }
}

也许你能帮我。

谢谢!

最简单快捷的方法是在关闭通知时从后端数据库中删除设备令牌

通知可能 disabled/enabled 来自设备本身,如下所示:

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

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