读取“设置”中的通知标志我的 iPhone 应用程序中的应用程序

发布于 2024-10-25 02:48:15 字数 104 浏览 2 评论 0原文

我正在为我的应用程序启用推送通知。当我的应用程序运行时,我们如何读取“设置”应用程序中的通知标志。由于某些原因,我需要知道特定通知(警报、声音、徽章)是否设置为开/关。

请指导。

I am enabling push notification for my application. How can we read the flags for notification in "Settings" app when my app is Running. For some reasons, I need to know whether a particular notification (alert, sound, badge) is set to ON/OFF.

Please guide.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

手心的温暖 2024-11-01 02:48:15

尝试调用此方法[[UIApplication共享应用]enabledRemoteNotificationTypes]

它将返回一个UIRemoteNotificationType,您可以使用它来确定可用的内容。

UIRemoteNotificationType status = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];

现在,可以使用 NSLog(@"status = ", status); 将状态视为 int,我们可以准确地确定正在发生的情况。但要做到这一点,我们需要了解 UIRemoteNotificationType。

typedef enum {
   UIRemoteNotificationTypeNone    = 0,
   UIRemoteNotificationTypeBadge   = 1 << 0,
   UIRemoteNotificationTypeSound   = 1 << 1,
   UIRemoteNotificationTypeAlert   = 1 << 2,
   UIRemoteNotificationTypeNewsstandContentAvailability = 1 << 3
} UIRemoteNotificationType;

无需赘述,您基本上需要了解的是...

  • 如果徽章打开,添加 1
  • 如果声音打开,添加 2
  • 如果警报打开,添加 4
  • 如果报亭内容可用,添加 8 (我不会担心这个人)

假设您想知道徽章/声音/警报是否全部打开。 UIRemoteNotificationType(如果您正在玩的话,则为状态)应该为 7。

现在,让我们倒退一下。假设status == 5。只有一种设置配置可以为我们提供此值,即徽章和警报是否打开(徽章加 1,警报加 4,总计为 5)并且声音关闭。

如果status == 6怎么办?同样,只有一种设置配置会返回此数字,即警报和声音均打开,而徽章关闭。

使用 IF 语句,我们可以执行类似的操作

If (status == 5)
{
    NSLog(@"User has sound alerts disabled");
    [self fireThatSpecialMethod];
}

:运行一组代码,或者在禁用声音但其他一切都打开时触发特定方法。无论如何,希望这个回答对人们有所帮助!

Try evoking this method [[UIApplication sharedApplication] enabledRemoteNotificationTypes]

It will return a UIRemoteNotificationType which you can work with to determine what is available.

UIRemoteNotificationType status = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];

Now, status can be looked at as an int using NSLog(@"status = ", status);, to which we can determine exactly what is on. But to do this we need to understand UIRemoteNotificationType.

typedef enum {
   UIRemoteNotificationTypeNone    = 0,
   UIRemoteNotificationTypeBadge   = 1 << 0,
   UIRemoteNotificationTypeSound   = 1 << 1,
   UIRemoteNotificationTypeAlert   = 1 << 2,
   UIRemoteNotificationTypeNewsstandContentAvailability = 1 << 3
} UIRemoteNotificationType;

Without going into much detail, what you basically need to walk away from this knowing is that ...

  • If badges are on, add 1
  • If sound is on, add 2
  • If alerts are on, add 4
  • If Newsstand Content is available, add 8 (I'm not going to worry about this guy)

Let's say you want to know if badges/sound/alerts are all on. The UIRemoteNotificationType (status if you are playing along) should come out to be 7.

Now, lets work backwards. Lets say that status == 5. There is only one configuration of settings that can give us this value, and that is if badges and alerts are on (badges add 1, alerts add 4, total is 5) and sound is off.

What if status == 6? Again, there is only one configuration of settings that will return this number, and that is if alerts and sound are both on, while badges are off.

Using IF statements, we can do something like

If (status == 5)
{
    NSLog(@"User has sound alerts disabled");
    [self fireThatSpecialMethod];
}

To run a set block of code, or fire a particular method for when sound is disabled, but everything else is on. Anyways, hope this response is helpful to people!

享受孤独 2024-11-01 02:48:15

请注意,从 iOS 8 开始,您正在寻找的确定远程通知是否已注册的方法如下:

[[UIApplication sharedApplication] isRegisteredForRemoteNotifications]

您可以使用以下方法确定用户当前启用的通知类型 :以下方法

[[UIApplication sharedApplication] currentUserNotificationSettings]

将返回一个包含您需要的所有信息的 UIUserNotificationSettings 对象。

文档链接:

isRegisteredForRemoteNotifications

currentUserNotificationSettings

UIUserNotificationSettings

Note that as of iOS 8 the method you are looking for to determine if remote notifications are registered is this:

[[UIApplication sharedApplication] isRegisteredForRemoteNotifications]

You may determine what kinds of notifications the user currently has enabled using the following method

[[UIApplication sharedApplication] currentUserNotificationSettings]

This returns a UIUserNotificationSettings object with all of the info you need.

Documentation Links:

isRegisteredForRemoteNotifications

currentUserNotificationSettings

UIUserNotificationSettings

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文