Change push notifications programmatically in Swift

前端 未结 4 2075
天涯浪人
天涯浪人 2021-01-05 16:08

I have an app where I have implemented push notifications. I check with the user to allow remote notifications with:

let settings = UIUserNotificationSetting         


        
相关标签:
4条回答
  • 2021-01-05 16:35

    if you are using FireBase to send push notifications to your devices , you can use topic subscription to enable push notification in subscribed devices and unsubscribe users from the topic when you don't want the user to receive push notification in those devices that have been unsubscribed.

    to subscribe a user to a topic simply import Firebase, then use this method:

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

    and to unsubscribe a user use:

    Messaging.messaging().unsubscribe(fromTopic: "topicName")
    
    0 讨论(0)
  • 2021-01-05 16:35
    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)
                    }
                }
            }
    
    0 讨论(0)
  • 2021-01-05 16:43

    There isn't any way you can change push notifications permission status from program. Also, prompt asking user to allow push notifications can not be shown again and again. You can refer this https://developer.apple.com/library/ios/technotes/tn2265/_index.html.

    The first time a push-enabled app registers for push notifications, iOS asks the user if they wish to receive notifications for that app. Once the user has responded to this alert it is not presented again unless the device is restored or the app has been uninstalled for at least a day.

    So using UISwitch to toggle permission status doesn't make any sense unless you use switch status to turn on/off remote notifications from your server.

    0 讨论(0)
  • 2021-01-05 16:55

    Updated with 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()
        }
    }
    
    0 讨论(0)
提交回复
热议问题