在 Swift 中以编程方式更改推送通知
Change push notifications programmatically in Swift
我有一个实现了推送通知的应用程序。我与用户确认是否允许远程通知:
let settings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(settings)
UIApplication.sharedApplication().registerForRemoteNotifications()
我还使用设备令牌将响应存储在数据库中。
我还在设置页面中实施了一个 UISwitch 以 enable/disable 推送通知,但是我只能在数据库列中 enable/disable 这个。
我的问题是,如果用户在初始请求中选择了“不允许”,我将无法在 phone 设置中启用推送通知,因此即使我在数据库中设置了启用值,通知永远不会达到 phone,因为 phone 设置仍设置为禁用。
Swift 2 中是否有一种方法可以从应用内更改 phone 设置中的推送通知,而不是用户必须进入设置才能更改?或者让 UISwitch 允许用户切换 on/off 推送通知是完全多余的吗?
您无法通过任何方式更改程序的推送通知权限状态。此外,提示询问用户是否允许推送通知不能一次又一次地显示。你可以参考这个 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.
因此,使用 UISwitch 切换权限状态没有任何意义,除非您使用切换状态从您的服务器打开 on/off 远程通知。
更新为 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()
}
}
如果您使用 FireBase
向您的设备发送推送通知,您可以使用主题订阅在订阅的设备中启用推送通知,并在您不希望用户接收时取消订阅该主题在那些已取消订阅的设备中推送通知。
要为用户订阅主题,只需导入 Firebase,然后使用此方法:
Messaging.messaging().subscribe(toTopic: "topicName")
要退订用户,请使用:
Messaging.messaging().unsubscribe(fromTopic: "topicName")
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)
}
}
}
我有一个实现了推送通知的应用程序。我与用户确认是否允许远程通知:
let settings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(settings)
UIApplication.sharedApplication().registerForRemoteNotifications()
我还使用设备令牌将响应存储在数据库中。
我还在设置页面中实施了一个 UISwitch 以 enable/disable 推送通知,但是我只能在数据库列中 enable/disable 这个。
我的问题是,如果用户在初始请求中选择了“不允许”,我将无法在 phone 设置中启用推送通知,因此即使我在数据库中设置了启用值,通知永远不会达到 phone,因为 phone 设置仍设置为禁用。
Swift 2 中是否有一种方法可以从应用内更改 phone 设置中的推送通知,而不是用户必须进入设置才能更改?或者让 UISwitch 允许用户切换 on/off 推送通知是完全多余的吗?
您无法通过任何方式更改程序的推送通知权限状态。此外,提示询问用户是否允许推送通知不能一次又一次地显示。你可以参考这个 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.
因此,使用 UISwitch 切换权限状态没有任何意义,除非您使用切换状态从您的服务器打开 on/off 远程通知。
更新为 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()
}
}
如果您使用 FireBase
向您的设备发送推送通知,您可以使用主题订阅在订阅的设备中启用推送通知,并在您不希望用户接收时取消订阅该主题在那些已取消订阅的设备中推送通知。
要为用户订阅主题,只需导入 Firebase,然后使用此方法:
Messaging.messaging().subscribe(toTopic: "topicName")
要退订用户,请使用:
Messaging.messaging().unsubscribe(fromTopic: "topicName")
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)
}
}
}