我正在尝试注册类别,但由于某种原因我无法在 iPhone 上注册,但它可以在模拟器上运行?我使用以下代码进行注册,然后检查我订阅了哪些类别。当我在 iPhone 上运行应用程序时进行检查时,它显示 0,当我在模拟器上检查时,它显示 1。
let snoozeAction = UNNotificationAction(identifier: "SNOOZE_ACTION",
title: "Snooze",
options: .foreground)
let expiredCategory = UNNotificationCategory(identifier: "TIMER_EXPIRED",
actions: [snoozeAction],
intentIdentifiers: [],
options: UNNotificationCategoryOptions(rawValue: 0))
let center = UNUserNotificationCenter.current()
center.setNotificationCategories([expiredCategory])
然后我检查
UNUserNotificationCenter.current().getNotificationCategories { categories in
print("These are the categories\(categories.count)")
print(categories.description)
}
我不知道发生了什么!
最佳答案
Try this, I am using this and its working for me.
func notificationSetup()
{
if #available(iOS 10.0, *)
{
let center = UNUserNotificationCenter.current()
center.delegate = self
center.requestAuthorization(options: [.alert,.badge,.sound]) { granted,error in
if (error == nil) {
UIApplication.shared.registerForRemoteNotifications()
}
}
let snoozeAction = UNNotificationAction(identifier: "SNOOZE_ACTION", title: "Snooze", options: [.foreground, .authenticationRequired])
let categoryOptions = UNNotificationCategoryOptions(rawValue: 0)
let expiredCategory = UNNotificationCategory(identifier: "TIMER_EXPIRED", actions: [snoozeAction], intentIdentifiers: [], options: categoryOptions)
let categories = Set([expiredCategory])
center.setNotificationCategories(categories)
}
}
关于ios - 如何注册通知类别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46352959/