我正在开发一个带有推送通知的应用程序。为了检查所有可能的用户交互方式,我想在用户在第一次启动时拒绝为我的应用启用推送通知时测试我的应用。
但是,对话框(由 启动registerForRemoteNotificationTypes
)在每个应用程序中仅出现一次。如何重置我的应用程序的 iPhone OS 内存。删除应用程序并重新安装没有帮助。
我正在开发一个带有推送通知的应用程序。为了检查所有可能的用户交互方式,我想在用户在第一次启动时拒绝为我的应用启用推送通知时测试我的应用。
但是,对话框(由 启动registerForRemoteNotificationTypes
)在每个应用程序中仅出现一次。如何重置我的应用程序的 iPhone OS 内存。删除应用程序并重新安装没有帮助。
Technical Note TN2265: Troubleshooting Push Notifications
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 setting the system clock forward a day or more, turning the device off completely, then turning the device back on.
Update: As noted in the comments below, this solution stopped working since iOS 5.1. I would encourage filing a bug with Apple so they can update their documentation. The current solution seems to be resetting the device's content and settings.
Update: The tech note has been updated with new steps that work correctly as of iOS 7.
- Delete your app from the device.
- Turn the device off completely and turn it back on.
- Go to Settings > General > Date & Time and set the date ahead a day or more.
- Turn the device off completely again and turn it back on.
UPDATE as of iOS 9
Simply deleting and reinstalling the app will reset the notification status to notDetermined
(meaning prompts will appear).
Thanks to the answer by Gomfucius below: https://stackoverflow.com/a/33247900/704803
另一个仅用于测试的解决方案是简单地更改您的捆绑包 ID。完成后别忘了把它改回来!
在 iOS 9.0.2 上,每次删除应用程序并重新安装它时,我都会收到“注册推送通知警报”。这对于 AppStore 生产下载和 adhoc 模式都是如此。
更新:已确认这适用于 iOS 9.x
如前所述,iOS5 更新了为设备上的应用程序重置通知状态的方法。
这在 iOS6 上适用于我:
但是,这只会使初始提示再次出现 - 它不会删除任何其他与推送状态相关的内容。
plist:/private/var/mobile/Library/RemoteNotification/Clients.plist
... 包含推送通知的注册客户端。删除您的应用程序的条目将导致提示重新出现
以编程方式进行似乎每次都对我有用。我有一个未注释以下行的构建:
[[UIApplication sharedApplication] unregisterForRemoteNotifications];
每次我想从 PN 注销时我都会运行它。您可能必须从最近列表中明确结束该应用程序,并在“设置”应用程序中使用“通知中心”以使其正确。
此外,要求用户注册 PN 的 UI 提示可能不会出现。不确定是否已在任何最近的 iOS 版本中被禁用。
I agree with micmdk.. I had a development environment setup with Push Notifications and needed a way to reset my phone to look like an initial install… and only these precise steps worked for me… requires TWO reboots of Device:
From APPLE TECH DOC:
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:
Delete your app from the device.
Turn the device off completely and turn it back on.
Go to Settings > General > Date & Time and set the date ahead a day or more.
Turn the device off completely again and turn it back on.
正如 ianolito 所说,设置日期应该有效:
您可以通过将系统时钟提前一天或更长时间,完全关闭设备,然后重新打开设备来实现后者,而无需实际等待一天。
我注意到在我的设备(iPhone 4、iOS 6.1.2)上将系统时钟提前一天甚至几天对我来说都不起作用。所以我将日期提前了一个月,然后它确实起作用了,我的应用程序再次显示了通知提示。
希望这对任何人都有帮助,它可能有点头疼!
The same tech note as refered to in the accepted answer (TN2265 - Troubleshooting Push Notifications) has since been updated with a solution for iOS 5 and above.
In short: create a backup and restore from it every time.
On iOS 5 and later, reset the push notifications permissions alert by restoring the device from a backup (r. 11450187). Here are the steps to do this efficiently:
- Use the Xcode Organizer to install your app on the device. The key is to install the app for the first time without running it.
- Use iTunes to back up the device.
- Run the app. The push notifications permissions alert will be presented.
- When you want to reset the push notifications permissions alert, restore the device from the backup you created in the first step.
我过去曾对此感到疑惑,并得出结论,它实际上不是我的代码的有效测试用例。我认为您的应用程序代码实际上无法区分某人第一次拒绝通知或后来从 iPhone 通知设置中禁用它。确实,用户体验有所不同,但这隐藏在对 registerForRemoteNotificationTypes 的调用中。
调用 unregisterForRemoteNotifications 不会完全从通知设置中删除应用程序 - 尽管它确实删除了该应用程序设置的内容。所以这仍然不会导致下次应用程序运行时对话框再次显示给用户(至少不是在我目前正在测试的 v3.1.3 上)。但正如我上面所说,您可能不应该担心这一点。
Apple 技术说明还描述了您可以恢复设备以重置推送通知对话框。
它并不是说您也可以在设备本身(iOS 5.x)上使用“常规 -> 重置 -> 删除所有内容和设置”选项。
我最近在 react-native 应用程序中遇到了类似的问题。iPhone OS 版本为 13.1 我卸载了该应用程序并尝试安装该应用程序,发现没有提示位置和通知权限。
在检查设置时,我可以看到我的应用程序已启用位置(从以前的安装)但是没有针对通知的相应条目尝试卸载并重新启动而不设置时间,它不起作用。顺便说一句,我也尝试下载 Appstore 应用程序,仍然是相同的行为。
该问题仅在设置设备时间后才得到解决。
除了ianolito的答案。
我一年前下载的应用程序也有同样的问题,最初拒绝推送通知。现在想要推送通知,这些步骤在 iOS 7 beta 上对我有用。不确定是哪个点准确触发了它。
感谢上帝,我不必“删除所有内容和设置”。也许它会帮助某人。
经过数小时的搜索,上面的建议没有运气,这对 3.x+ 来说就像一个魅力
override func viewDidLoad() {
super.viewDidLoad()
requestAuthorization()
}
func requestAuthorization() {
if #available(iOS 10.0, *) {
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { (granted, error) in
print("Access granted: \(granted.description)")
}
} else {
// Fallback on earlier versions
}
}