ios - 如何正确禁用/启用推送通知

标签 ios swift firebase push-notification

各位开发人员大家好。

我正在尝试使用 UISwitch 创建一个操作,允许用户禁用/启用他们的推送通知。当我关闭开关时,我不再收到推送通知。但是,如果我尝试重新打开它,我仍然没有收到它。也使用 Firebase。

这是我的应用程序委托(delegate)代码。我会尝试分享相关代码,但如果您需要,请随时请求更多代码。

PS:我用于切换器的代码实际上来 self 在 StackOverFlow 上找到的答案。

// App delegate

  func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        print("Registered for notifications", deviceToken)
        Messaging.messaging().apnsToken = deviceToken

    }

    func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
        print("Registered with FCM with token:", fcmToken)
    }

  private func attemptRegisterForNotifications(application: UIApplication) {
        // APNS means Apple push notification services
        print("Attempting to register APNS...")

        Messaging.messaging().delegate = self
        UNUserNotificationCenter.current().delegate = self



        // user notification auth
        // all of this works for iOs 10+
        let options: UNAuthorizationOptions = [.alert, . badge, .sound]
        UNUserNotificationCenter.current().requestAuthorization(options: options) { (granted, error) in
            if let error = error {
                print("Failed to request auth:", error)
                return
            }

            if granted {
                print("Auth granted.")
            } else {
                print("Auth denied")
            }

        }

        application.registerForRemoteNotifications()


    }

这是我在设置 View Controller 中的代码


   self.notificationSwitch.addTarget(self, action: #selector(action(sender:)), for: .valueChanged)...

 @objc func action(sender: UISwitch) {

        let userDefaults = UserDefaults.standard
        userDefaults.set(sender.isOn, forKey:"identifier")

        if(notificationSwitch.isOn) {
            print("Switch is on")
            UIApplication.shared.registerForRemoteNotifications()

        } else {

            print("Switch is off")
            UIApplication.shared.unregisterForRemoteNotifications()
        }

    }

最佳答案

根据 unregisterForRemoteNotifications 的文档:

You should call this method in rare circumstances only, such as when a new version of the app removes support for all types of remote notifications. Users can temporarily prevent apps from receiving remote notifications through the Notifications section of the Settings app. Apps unregistered through this method can always re-register.

正确的方法(实际上是我在做过的项目中看到的方法)是调用api并告诉后端不应该发送推送通知。

关于ios - 如何正确禁用/启用推送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57457509/

相关文章:

javascript - ReactJS firebase.initializeApp 不是函数

ios - NSMutableArray 中的内存管理

ios - Swift 3 - 重用具有多个自定义单元格的单元格时出现问题

ios - 无法使用类型为“(UInt32, UnsafePointer<Int8>, UInt, [CChar], Int, UnsafeMutablePointer<CUnsignedChar>) 的参数列表调用 'CCHmac'

ios - 尝试以编程方式添加 UITableView() 时未在 UIViewController 中显示

ios - 根据(城市)从 firebase 检索数据

ios - 在 Firebase 中更改值后删除观察者

ios - 我无法将屏幕截图上传到新的 iTunes Connect 媒体管理器

javascript - 卸载某些页面的js请求

objective-c - 为图像添加色调