【问题标题】:How to disconnect Firebase Notifications on iOS (Swift 3)?如何在 iOS(Swift 3)上断开 Firebase 通知?
【发布时间】:2017-06-26 09:01:50
【问题描述】:

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

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

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

也许你可以帮助我。

谢谢!

【问题讨论】:

    标签: ios swift firebase firebase-notifications


    【解决方案1】:

    可以像这样从设备本身禁用/启用通知:

        func switchChanged(sender: UISwitch!) {
        print("Switch value is \(sender.isOn)")
    
        if(sender.isOn){
            UIApplication.shared.registerForRemoteNotifications()
        }
        else{
            UIApplication.shared.unregisterForRemoteNotifications()
        }
    }
    

    【讨论】:

    • 谢谢希瓦姆·特里帕蒂。你救了我的一天。非常感谢。
    • 我也无法正常工作,即使使用 unregisterForRemoteNotifications,fcm 仍然可以发送
    【解决方案2】:

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

    【讨论】:

    • Akash,在安卓设备上也是一样的吗?我可以在 Android 中删除令牌吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-05
    相关资源
    最近更新 更多