【问题标题】:Change push notifications programmatically in Swift在 Swift 中以编程方式更改推送通知
【发布时间】:2015-11-04 09:17:34
【问题描述】:

我有一个应用,我在其中实现了推送通知。我与用户核对以允许远程通知:

let settings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(settings)
UIApplication.sharedApplication().registerForRemoteNotifications()

我还将响应与设备令牌一起存储在数据库中。

我还在设置页面中实现了一个 UISwitch 来启用/禁用推送通知,但是我只能在 DB 列中启用/禁用它。

我的问题是,如果用户在初始请求中选择了不允许,我无法在手机设置中启用推送通知,因此即使我在数据库中设置了启用的值,通知也永远不会到达手机手机设置仍然设置为禁用。

在 Swift 2 中是否有一种方法可以在应用程序内更改手机设置中的推送通知,而不是用户必须进入设置进行更改?还是让 UISwitch 允许用户打开/关闭推送通知是完全多余的?

【问题讨论】:

    标签: ios swift notifications swift2


    【解决方案1】:

    您无法通过程序更改推送通知权限状态。此外,不能一次又一次地显示要求用户允许推送通知的提示。你可以参考这个https://developer.apple.com/library/ios/technotes/tn2265/_index.html

    启用推送的应用首次注册推送通知时,iOS 会询问用户是否希望接收该应用的通知。一旦用户对此警报做出响应,除非设备已恢复或应用已卸载至少一天,否则不会再次显示。

    所以使用 UISwitch 来切换权限状态没有任何意义,除非您使用切换状态来打开/关闭来自服务器的远程通知。

    【讨论】:

    • 谢谢。也许我可以检查切换切换时通知是否被禁用,并提醒用户他们需要在设置中启用它们。
    【解决方案2】:

    用 swift 4 更新:

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

    【讨论】:

      【解决方案3】:

      如果您使用FireBase 向您的设备发送推送通知,您可以使用主题订阅在订阅的设备中启用推送通知,并在您不希望用户在这些设备中接收推送通知时取消订阅用户的主题已取消订阅的设备。

      要让用户订阅某个主题,只需导入 Firebase,然后使用此方法:

      Messaging.messaging().subscribe(toTopic: "topicName")
      

      并取消订阅用户使用:

      Messaging.messaging().unsubscribe(fromTopic: "topicName")
      

      【讨论】:

        【解决方案4】:
        if settings.authorizationStatus != .authorized{
        
                    // Either denied or notDetermined
                    // Ask the user to enable it in settings.
        
                    UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) {
                        (granted, error) in
                          // add your own
                        UNUserNotificationCenter.current().delegate = self
                        let alertController = UIAlertController(title: appName, message: "Please enable notifications in settings and try again.", preferredStyle: .alert)
                        let settingsAction = UIAlertAction(title: "Settings", style: .default) { (_) -> Void in
                            guard let settingsUrl = URL(string: UIApplication.openSettingsURLString) else {
                                return
                            }
                            if UIApplication.shared.canOpenURL(settingsUrl) {
                                UIApplication.shared.open(settingsUrl, completionHandler: { (success) in
                                })
                            }
                        }
                        let cancelAction = UIAlertAction(title: "Cancel", style: .default, handler: nil)
                        alertController.addAction(cancelAction)
                        alertController.addAction(settingsAction)
                        DispatchQueue.main.async {
                            (UIApplication.shared.delegate as? AppDelegate)?.window?.rootViewController?.present(alertController, animated: true, completion: nil)
                        }
                    }
                }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-08-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多