ios - 如何注册通知类别?

标签 ios iphone swift push-notification notifications

我正在尝试注册类别,但由于某种原因我无法在 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/

相关文章:

iphone - 如何实现具有可互换中心面板的 JASidePanel?

iphone - 在 UIScrollView 中使用 CATiledLayer 进行缩放

iphone - iPhone 应用程序在存储数据时如何交互?

iphone - 使用 NSUserDefaults 的正确方法?

ios - 如何转到导致 xcode 错误的行

ios - swift 3.0 : "objc_setAssociatedObject" issue

ios - 将地址簿导出到 vcard 文件

ios - 为什么我总是必须将 Sprite 的位置更改为其他位置才能获得所需的工作位置

ios - 如何在 iPhone 应用程序中实现 NFC?

regex - 在swift中使用正则表达式从句子内部提取单词