在询问并授予 iOS 通知权限时检测? [英] Detect when asked and granted permission to iOS notifications?

查看:40
本文介绍了在询问并授予 iOS 通知权限时检测?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 iOS 客户端中,当您第一次运行它时,您会收到一个 UIAlert,要求授予访问权限以发送此人通知.

In the iOS client when you first run it you get a UIAlert asking for permission to grant access to send this person notifications.

我想知道是否可以先:

  1. 赶上此警报何时触发(在它发生之前)
  2. 了解给出的响应(取消或确定)(发生后)

有没有这样的方式来获取以上两种场景的回调?

Is there such a way to get callbacks for the above two scenarios?

非常感谢

推荐答案

在 iOS 8 及更高版本上,在您的 AppDelegate 中实现以下方法:

On iOS 8 and later, implement the following method in your AppDelegate:

- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings {

    // You could check here [[UIApplication sharedApplication] currentUserNotificationSettings]

    // This is where you set up your local notification
}

这个方法是一个 UIApplicationDelegate 方法,所以你需要做的就是在你的 MyAppDelegate.m 文件中实现它.

This method is a UIApplicationDelegate method, so all you need to do is implement it in your MyAppDelegate.m file.

来自 文档:

告诉代理可以使用哪些类型的通知来引起用户的注意.

Tells the delegate what types of notifications may be used to get the user’s attention.

参数

application:注册用户通知设置的应用对象.

application: The app object that registered the user notification settings.

notificationSettings:您的应用可用的用户通知设置.

notificationSettings: The user notification settings that are available to your app.

此对象中的设置可能与您最初请求的设置不同.

The settings in this object may be different than the ones you originally requested.

讨论

使用本地或远程通知来提醒用户注意新信息的应用程序必须通过调用应用程序对象的 registerUserNotificationSettings: 方法来注册他们想要使用的通知类型.(在 8.0 之前的 iOS 版本上链接的应用程序中,当您安排本地通知时,注册也可能隐式发生.)您的应用程序的请求与用户当前的偏好相结合,以确定允许哪些通知类型并将结果传送到此NotificationSettings 参数中的方法.

Apps that use local or remote notifications to alert the user to new information must register the types of notifications they want to use by calling the registerUserNotificationSettings: method of the app object. (In apps that link on versions of iOS prior to 8.0, registration can also happen implicitly when you schedule a local notification.) Your app’s request is combined with the user’s current preferences to determine what notification types are allowed and the results are delivered to this method in the notificationSettings parameter.

您第一次注册应用的首选通知类型时,系统会询问用户是否应允许您的应用发送通知并存储用户的响应.在随后的注册尝试期间,系统不会提示用户.用户可以随时使用设置"应用更改通知首选项.

The first time you register your app’s preferred notification types, the system asks the user whether your app should be allowed to deliver notifications and stores the user’s response. The system does not prompt the user during subsequent registration attempts. The user can always change the notification preferences using the Settings app.

因为用户的偏好可以改变,所以你应该经常检查 notificationSettings 参数的内容.这些设置仅控制是通知用户有关本地通知还是远程通知.通知仍会在适当的时间发送到您的应用.

Because the user’s preferences can change, you should always check the contents of the notificationSettings parameter. These settings control only whether the user is notified about a local or remote notification. The notification is still delivered to your app at the appropriate times.

这篇关于在询问并授予 iOS 通知权限时检测?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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