在 Swift 中以编程方式更改推送通知 [英] Change push notifications programmatically in Swift

查看:18
本文介绍了在 Swift 中以编程方式更改推送通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

I also store the response in a DB with the device token.

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

I have also implemented a UISwitch in a settings page to enable/disable push notifications however I have only been able to enable/disable this in the DB column.

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

My problem is that if a user selected Don't Allow in the initial request, I cannot enable push notifications in the phone settings so even if I set the value to enable in the DB, the notification will never reach the phone as the phone settings are still set to disable.

Swift 2 有没有办法从应用程序内更改手机设置中的推送通知,而无需用户进入设置进行更改?或者让 UISwitch 允许用户打开/关闭推送通知是完全多余的吗?

Is there a way in Swift 2 to change the push notification in the phone setting from within the app instead of the user having to go in to the settings to change? Or is it completely redundant to have a UISwitch allow the user to toggle on/off push notifications?

推荐答案

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

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.

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

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 切换权限状态没有任何意义,除非您使用切换状态打开/关闭来自服务器的远程通知.

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.

这篇关于在 Swift 中以编程方式更改推送通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆