按下按钮后如何提示用户获得推送通知权限? [英] How do I prompt the user for push notification permission after a button is pushed?

查看:38
本文介绍了按下按钮后如何提示用户获得推送通知权限?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

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

解决方案

首先,您应该只提示您的用户一次是否要接收推送通知.因此,将其绑定到一个按钮并尝试每次都运行它是一个糟糕的用户体验决策.仅当按钮实际执行更多需要启用推送通知的操作时,才将您的推送通知提示绑定到按钮.

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

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

如果您想重置推送通知权限出于测试目的Apple 说这是可能的:

<块引用>

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

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

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

  1. 从设备中删除您的应用.
  2. 完全关闭设备并重新打开.
  3. 转到设置">一般>日期 &将时间和日期提前一天或更长时间.
  4. 再次完全关闭设备并重新打开.

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

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

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 immediately when the app launched.

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

解决方案

First off, you should only prompt your user once on 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 UIApplication registerForRemoteNotifications method. 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 not be 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.

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

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.

这篇关于按下按钮后如何提示用户获得推送通知权限?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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