确定 iPhone 上用户是否启用了推送通知
我正在寻找一种方法来确定用户是否通过设置启用或禁用了我的应用程序的推送通知。
I'm looking for a way to determine if the user has, via settings, enabled or disabled their push notifications for my application.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(19)
调用enabledRemoteNotificationsTypes并检查掩码。
例如:
iOS8及以上版本:
Call
enabledRemoteNotificationsTypes
and check the mask.For example:
iOS8 and above:
Quantumpotato 的问题:
其中
types
是由一个可以使用的
代替
的给出的,它允许您仅检查通知是否已启用(并且不用担心声音、徽章、通知中心等)。如果“Alert Style”设置为“Banners”或“Alerts”,第一行代码 (
types & UIRemoteNotificationTypeAlert
) 将返回YES
,并且NO< /code> 如果“警报样式”设置为“无”,无论其他设置如何。
quantumpotato's issue:
Where
types
is given byone can use
instead of
will allow you to check only whether notifications are enabled (and don't worry about sounds, badges, notification center, etc.). The first line of code (
types & UIRemoteNotificationTypeAlert
) will returnYES
if "Alert Style" is set to "Banners" or "Alerts", andNO
if "Alert Style" is set to "None", irrespective of other settings.更新了 swift4.0、iOS11 的代码
更新了 swift3.0、iOS10 的代码
Updated code for swift4.0 , iOS11
Code for swift3.0 , iOS10
在最新版本的 iOS 中,此方法现已弃用。要同时支持 iOS 7 和 iOS 8,请使用:
In the latest version of iOS this method is now deprecated. To support both iOS 7 and iOS 8 use:
您将在下面找到涵盖 iOS8 和 iOS7(及更低版本)的完整示例。请注意,在 iOS8 之前,您无法区分“远程通知已禁用”和“仅在锁定屏幕中查看启用”。
Below you'll find a complete example that covers both iOS8 and iOS7 (and lower versions). Please note that prior to iOS8 you can't distinguish between "remote notifications disabled" and "only View in lockscreen enabled".
Swift 3+
iOS10+ 的 RxSwift Observable 版本:
Swift 3+
RxSwift Observable Version for iOS10+:
在尝试支持 iOS8 及更低版本时,我没有按照 Kevin 建议的那样使用
isRegisteredForRemoteNotifications
。相反,我使用了 currentUserNotificationSettings,它在我的测试中效果很好。In trying to support both iOS8 and lower, I didn't have much luck using
isRegisteredForRemoteNotifications
as Kevin suggested. Instead I usedcurrentUserNotificationSettings
, which worked great in my testing.不幸的是,这些解决方案都没有真正解决问题,因为归根结底,API 在提供相关信息方面严重缺乏。您可以做出一些猜测,但是使用
currentUserNotificationSettings
(iOS8+) 在当前形式下不足以真正回答问题。尽管这里的许多解决方案似乎表明要么 isRegisteredForRemoteNotifications 要么是更明确的答案,但实际上并非如此。考虑一下:
isRegisteredForRemoteNotifications
文档指出:但是,如果您将一个简单的
NSLog
放入您的应用程序委托中来观察行为,很明显这不会表现出来我们预期它会如何运作。它实际上直接与已为此应用程序/设备激活的远程通知相关。第一次激活后,将始终返回YES
。即使在设置(通知)中关闭它们仍然会导致返回YES
这是因为,从 iOS8 开始,应用程序可能会注册远程通知,甚至在用户未启用通知的情况下发送到设备,如果用户没有打开警报、徽章和声音,他们可能不会执行这些操作。无声通知是一个很好的例子,说明即使通知关闭,您也可以继续执行某些操作。就
currentUserNotificationSettings
而言,它指示以下四件事之一:警报已开启
徽章已开启
声音已打开
没有一个打开。
这绝对不会向您提供有关其他因素或通知开关本身的任何指示。
用户实际上可以关闭徽章、声音和警报,但仍然在锁屏或通知中心显示。该用户应该仍然可以接收推送通知,并且能够在锁定屏幕和通知中心看到它们。他们打开了通知开关。但在这种情况下,
currentUserNotificationSettings
将返回:UIUserNotificationTypeNone
。这并不真正代表用户的实际设置。人们可以做出一些猜测:
isRegisteredForRemoteNotifications
为NO
那么您可以假设该设备从未成功注册远程通知。application:didRegisterUserNotificationSettings:
进行回调,此时包含用户通知设置,因为这是用户第一次注册,设置应该< /em> 指示用户在权限请求方面选择的内容。如果设置等于UIUserNotificationTypeNone
以外的任何内容,则授予推送权限,否则将被拒绝。原因是,从您开始远程注册过程的那一刻起,用户只能接受或拒绝,接受的初始设置是您在注册过程中设置的设置。Unfortunately none of these solutions provided really solve the problem because at the end of the day the APIs are seriously lacking when it comes to providing the pertinent information. You can make a few guesses however using
currentUserNotificationSettings
(iOS8+) just isn't sufficient in its current form to really answer the question. Although a lot of the solutions here seem to suggest that either that orisRegisteredForRemoteNotifications
is more of a definitive answer it really is not.Consider this:
with
isRegisteredForRemoteNotifications
documentation states:However if you throw a simply
NSLog
into your app delegate to observe the behavior it is clear this does not behave the way we are anticipating it will work. It actually pertains directly to remote notifications having been activated for this app/device. Once activated for the first time this will always returnYES
. Even turning them off in settings (notifications) will still result in this returningYES
this is because, as of iOS8, an app may register for remote notifications and even send to a device without the user having notifications enabled, they just may not do Alerts, Badges and Sound without the user turning that on. Silent notifications are a good example of something you may continue to do even with notifications turned off.As far as
currentUserNotificationSettings
it indicates one of four things:Alerts are on
Badges are on
Sound is on
None are on.
This gives you absolutely no indication whatsoever about the other factors or the Notification switch itself.
A user may in fact turn off badges, sound and alerts but still have show on lockscreen or in notification center. This user should still be receiving push notifications and be able to see them both on the lock screen and in the notification center. They have the notification switch on. BUT
currentUserNotificationSettings
will return:UIUserNotificationTypeNone
in that case. This is not truly indicative of the users actual settings.A few guesses one can make:
isRegisteredForRemoteNotifications
isNO
then you can assume that this device has never successfully registered for remote notifications.application:didRegisterUserNotificationSettings:
is made containing user notification settings at this time since this is the first time a user has been registered the settings should indicate what the user selected in terms of the permission request. If the settings equate to anything other than:UIUserNotificationTypeNone
then push permission was granted, otherwise it was declined. The reason for this is that from the moment you begin the remote registration process the user only has the ability to accept or decline, with the initial settings of an acceptance being the settings you setup during the registration process.为了完成答案,它可以像这样工作......
编辑:这是不对的。因为这些是按位的东西,所以它不能与开关一起使用,所以我最终使用了这个:
To complete the answer, it could work something like this...
edit: This is not right. since these are bit-wise stuff, it wont work with a switch, so I ended using this:
对于 iOS7 及之前的版本,您确实应该使用
enabledRemoteNotificationTypes
并检查它是否等于(或不等于,具体取决于您想要的)UIRemoteNotificationTypeNone
。然而,对于 iOS8,仅使用上述许多状态的
isRegisteredForRemoteNotifications
检查并不总是足够的。您还应该检查application.currentUserNotificationSettings.types
是否等于(或不等于,具体取决于您想要的)UIUserNotificationTypeNone
!即使
currentUserNotificationSettings.types
返回UIUserNotificationTypeNone
,isRegisteredForRemoteNotifications
也可能返回 true。For iOS7 and before you should indeed use
enabledRemoteNotificationTypes
and check if it equals (or doesn't equal depending on what you want) toUIRemoteNotificationTypeNone
.However for iOS8 it is not always enough to only check with
isRegisteredForRemoteNotifications
as many state above. You should also check ifapplication.currentUserNotificationSettings.types
equals (or doesn't equal depending on what you want)UIUserNotificationTypeNone
!isRegisteredForRemoteNotifications
might return true even thoughcurrentUserNotificationSettings.types
returnsUIUserNotificationTypeNone
.这里我们从 UIApplication 获取 UIRemoteNotificationType。它代表了该应用程序在设置中的推送通知状态,您可以轻松查看其类型
Here we get the UIRemoteNotificationType from UIApplication. It represents the state of push notification of this app in the setting, than you can check on its type easily
我尝试使用 @Shaheen Ghiassy 提供的解决方案支持 iOS 10 及更高版本,但发现剥夺问题
enabledRemoteNotificationTypes
。因此,我找到的解决方案是使用isRegisteredForRemoteNotifications
而不是 iOS 8 中已弃用的enabledRemoteNotificationTypes
。下面是我的更新后的解决方案,非常适合我:我们可以轻松调用此函数并访问其
Bool
值,并可以通过以下方式将其转换为字符串值:希望它也能帮助其他人:)
快乐编码。
I try to support iOS 10 and above using solution provide by @Shaheen Ghiassy but find deprivation issue
enabledRemoteNotificationTypes
. So, the solution I find by usingisRegisteredForRemoteNotifications
instead ofenabledRemoteNotificationTypes
which deprecated in iOS 8. Below is my updated solution that worked perfectly for me:And we can call this function easily and be accessing its
Bool
value and can convert it into the string value by this:Hope it will help others too :)
Happy coding.
尽管 Zac 的答案在 iOS 7 之前是完全正确的,但自从 iOS 8 出现以来,情况发生了变化。因为从 iOS 8 开始,enabledRemoteNotificationTypes 已被弃用。对于 iOS 8 及更高版本,您需要使用 isRegisteredForRemoteNotifications。
Though Zac's answer was perfectly correct till iOS 7, it has changed since iOS 8 arrived. Because enabledRemoteNotificationTypes has been deprecated from iOS 8 onwards. For iOS 8 and later, you need to use isRegisteredForRemoteNotifications.
这个Swifty解决方案对我来说效果很好(iOS8+),
方法:
用法:
参考
This Swifty solution worked well for me (iOS8+),
Method:
Usage:
Ref
回复:
这是正确的
,但以下也是正确的! (因为 UIRemoteNotificationTypeNone 是 0 )
请参阅以下内容
re:
this is correct
but following is correct too ! (as UIRemoteNotificationTypeNone is 0 )
see the following
以下是如何在 Xamarin.ios 中执行此操作。
如果您支持 iOS 10+,则仅使用 UNUserNotificationCenter 方法。
Here's how to do this in Xamarin.ios.
If you are supporting iOS 10+ only go with the UNUserNotificationCenter method.
在 Xamarin 中,上述所有解决方案都不适合我。
这就是我使用的:
在您更改“设置”中的通知状态后,它也会获得实时更新。
In Xamarin, all above solution does not work for me.
This is what I use instead:
It's getting a live update also after you've changed the notification status in Settings.
从 @ZacBowling 的解决方案构建的完整轻松复制和粘贴代码 (https://stackoverflow.com/a/1535427/2298002)
这也将使用户进入您的应用程序设置并允许他们立即启用
我还添加了一个解决方案来检查位置服务是否已启用(并且也进入设置)
GLHF!
Full easy copy and paste code built from @ZacBowling's solution (https://stackoverflow.com/a/1535427/2298002)
this will also bring the user to your app settings and allow them to enable immediately
I also added in a solution for checking if location services is enabled (and brings to settings as well)
GLHF!