如何防止注册通知设置两次?

How to prevent registering notification settings twice?

我在我的 AppDelegate.didFinishLaunchingWithOptions 活动中注册通知设置,如下所示:

let notificationType = UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound
let acceptAction = UIMutableUserNotificationAction()
acceptAction.identifier = "Accept"
acceptAction.title = "Accept"
acceptAction.activationMode = UIUserNotificationActivationMode.Background
acceptAction.destructive = false
acceptAction.authenticationRequired = false

let declineAction = UIMutableUserNotificationAction()
declineAction.identifier = "Decline"
declineAction.title = "Decline"
declineAction.activationMode = UIUserNotificationActivationMode.Background
declineAction.destructive = false
declineAction.authenticationRequired = false

let category = UIMutableUserNotificationCategory()
category.identifier = "invite"
category.setActions([acceptAction, declineAction], forContext: UIUserNotificationActionContext.Default)
let categories = NSSet(array: [category])
let settings = UIUserNotificationSettings(forTypes: notificationType, categories: categories)
application.registerUserNotificationSettings(settings)

如何防止我的应用程序注册两次,以防用户退出应用程序并 returns 之后,或者重新注册多次是否无害?

一旦用户收到一次通知提示,就不会再次询问他们。您可以在模拟器中自行验证这一点。因此,那里的代码可以按原样使用。