加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ios – 按下按钮后如何提示用户提供推送通知权限?

发布时间:2020-12-14 19:41:42 所属栏目:百科 来源:网络整理
导读:我读了 How to control when to prompt user for push notification permissions in iOS和类似的问题,但它们不是用Swift 3编写的. 我已经读了五次Registering for Push Notifications in Xcode 8/Swift 3.0?. 我一遍又一遍地在模拟器中运行我的应用程序但是
我读了 How to control when to prompt user for push notification permissions in iOS和类似的问题,但它们不是用Swift 3编写的.

我已经读了五次Registering for Push Notifications in Xcode 8/Swift 3.0?.

我一遍又一遍地在模拟器中运行我的应用程序但是在第一次之后,应用程序从未提示用户提供推送通知权限.

我使用了Registering for Push Notifications in Xcode 8/Swift 3.0?中的代码并继续尝试不同的答案并将代码移动到不同的位置,这样只有在按下某个按钮后才会要求用户许可,而不是在应用程序启动时立即.

我是否必须使用Apple ID等注册推送通知代码才能在Simulator中工作?

解决方法

首先,您应该只提示您的用户是否想要获得推送通知.因此,将其绑定到按钮并尝试每次都运行它是一个糟糕的用户体验决定.如果按钮实际上执行了更多需要启用推送通知的内容,则只将推送通知提示绑定到按钮.

要注册用户以进行推送通知,您应该使用UIApplication registerForRemoteNotifications方法.运行此方法时,系统会提示用户是否要接收推送通知 – 但只提示一次!他们设置的设置将被视为确定,并且不会再次提示您的用户.如果他们想要开始接收推送通知,他们必须在应用程序的设置中更改它.这是预期的行为,你应该怎么做.

如果您只想在按下按钮时提示用户,则从应用程序的启动代码中删除对registerForRemoteNotifications的任何调用,并从绑定到按钮的操作中调用该方法.

如果要重置推送通知权限以进行测试,请参见this is how Apple says it’s possible:

Resetting the Push Notifications Permissions Alert on 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.

If you want to simulate a first-time run of your app,you can leave the app uninstalled for a day. You can achieve the latter without actually waiting a day by following these steps:

  1. Delete your app from the device.
  2. Turn the device off completely and turn it back on.
  3. Go to Settings > General > Date & Time and set the date ahead a day or more.
  4. Turn the device off completely again and turn it back on.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读