在我的应用中,我发送无提示推送通知"content-available": 1
。
在接收方,他们可以转到应用程序内的设置,然后打开通知和声音。
我知道我可以使用此代码来找出应用内任何一个的状态
UNUserNotificationCenter.current().getNotificationSettings { (settings: UNNotificationSettings) in
guard settings.authorizationStatus == .authorized else { return }
let soundSetting: UNNotificationSetting = settings.soundSetting
switch soundSetting {
case .enabled:
print("enabled")
case .disabled:
print("disabled")
case .notSupported:
print("???")
}
let badgeSetting: UNNotificationSetting = settings.badgeSetting
switch badgeSetting {
case .enabled:
print("enabled")
case .disabled:
print("disabled")
case .notSupported:
print("???")
}
UIApplication.shared.registerForRemoteNotifications()
}
问题是,如何在应用内部将notification
本身或sound
的状态更改为不将用户发送到应用外部?
lazy var switchNotificationControl: UISwitch = { ... }
lazy var switchSoundControl: UISwitch = { ... }
@objc func switchValueDidChange(_ sender: UISwitch) {
if (sender.isOn == true) {
// set notifications to on/true or sound to on/true
} else {
// set notifications to off/false or sound to off/false
}
}
答案 0 :(得分:0)
似乎没有可能从应用程序内部完成此操作
https://stackoverflow.com/a/33520827/4833705
https://stackoverflow.com/a/36654919/4833705
我还尝试了下面的代码,当(从应用程序外部)打开通知时,我使用它来将它们关闭,但它什么也没做。
@objc func switchValueDidChange(_ sender: UISwitch) {
if (sender.isOn == true) {
// *** doesn't work ***
UIApplication.shared.registerForRemoteNotifications()
} else {
// *** doesn't work ***
UIApplication.shared.unregisterForRemoteNotifications()
}
}
答案 1 :(得分:0)
用户接受或拒绝推送通知权限后,似乎无法以编程方式对其进行更改。您可以参考此Application Image。
首次启用推送的应用程序注册推送通知,iOS询问用户是否希望接收该应用程序的通知。用户响应此警报后,除非恢复设备或将应用程序卸载至少一天,否则不会再次显示该警报。
因此,打开/关闭推送通知的更好方法最好是在服务器上进行。