xcode 按下按钮后如何提示用户获得推送通知权限?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43625614/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 09:56:11  来源:igfitidea点击:

How do I prompt the user for push notification permission after a button is pushed?

iosxcodeswift3xcode8

提问by jfjldsfjljfkls

I read How to control when to prompt user for push notification permissions in iOSand similar questions but they weren't written in Swift 3.

我阅读了如何控制何时在 iOS和类似问题中提示用户推送通知权限,但它们不是用 Swift 3 编写的。

I have read Registering for Push Notifications in Xcode 8/Swift 3.0?five times.

我已阅读在 Xcode 8/Swift 3.0 中注册推送通知?五次。

I ran my app in Simulator over and over but after the first time, the app never prompted the user for push notification permission.

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

I used the code from Registering for Push Notifications in Xcode 8/Swift 3.0?and kept trying different answers and moving the code to different places so that the user would be asked for permission only after they pressed a certain button, and not immediatelywhen the app launched.

在 Xcode 8/Swift 3.0 中使用了注册推送通知中的代码并不断尝试不同的答案并将代码移动到不同的地方,这样用户只有在按下某个按钮后才会被请求许可,而不是在应用程序启动时立即请求许可

Do I have to be registered with an Apple ID etc. for the push notification code to even workin Simulator?

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

回答by Pedro Castilho

First off, you should only prompt your user onceon whether they want to get push notifications or not. So tying that to a button and trying to run it every time is a bad UX decision. Only tie your Push Notification prompt to a button if the button actually does more stuff which would require Push Notifications to be enabled.

首先,你应该只提示用户,一旦他们是否想要得到推送通知与否。因此,将其绑定到一个按钮并尝试每次都运行它是一个糟糕的用户体验决策。如果按钮实际上做了更多需要启用推送通知的事情,则仅将您的推送通知提示绑定到按钮。

To register your user for push notifications, you should use the UIApplicationregisterForRemoteNotificationsmethod. When this method runs, your user will be prompted on whether they want to receive push notifications - but only once! The setting they set will be considered definitive and your user will notbe prompted again. If they want to start receiving push notifications, they have to change that in the app's settings. This is expected behavior and is how you should do it.

要为推送通知注册您的用户,您应该使用该UIApplicationregisterForRemoteNotifications方法。当这个方法运行时,你的用户将被提示他们是否想要接收推送通知 - 但只有一次!他们设置的设置将被认为是确定的,并且不会再次提示您的用户。如果他们想开始接收推送通知,他们必须在应用程序的设置中进行更改。这是预期的行为,您应该这样做。

If you want to prompt the user only when they press a button, then remove any calls to registerForRemoteNotificationsfrom your app's startup code and call the method from the action tied to your button instead.

如果您只想在用户按下按钮时提示用户,请registerForRemoteNotifications从应用程序的启动代码中删除对 的任何调用,并从与按钮关联的操作中调用该方法。

If you want to reset the push notification permissions for testing purposes, 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.

在 iOS 上重置推送通知权限警报

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

如果您想模拟应用程序的首次运行,您可以将应用程序卸载一天。您可以按照以下步骤实现后者,而无需等待一天:

  1. 从设备中删除您的应用程序。
  2. 完全关闭设备并重新打开。
  3. 前往“设置”>“通用”>“日期与时间”,然后将日期提前一天或更长时间。
  4. 再次完全关闭设备并重新打开。