ios - 两种不同类别的本地通知

标签 ios swift xcode swift3 notifications

我创建了两个不同的函数,每个函数都创建了一个新的本地通知。

   func scheduleLocalNotification() {
    // Create Notification Content
    let notificationContent = UNMutableNotificationContent()

    // Configure Notification Content
    notificationContent.title = "Notif 1"
    notificationContent.subtitle = "Local Notifications"
    notificationContent.body = "hellow worlds."

    // Set Category Identifier
    notificationContent.categoryIdentifier = Notification.Category.tutorial

    // Add Trigger
    let notificationTrigger = UNTimeIntervalNotificationTrigger(timeInterval: 10.0, repeats: false)

    // Create Notification Request
    let notificationRequest = UNNotificationRequest(identifier: "cocoacasts_local_notification", content: notificationContent, trigger: notificationTrigger)

    // Add Request to User Notification Center
    UNUserNotificationCenter.current().add(notificationRequest) { (error) in
        if let error = error {
            print("Unable to Add Notification Request (\(error), \(error.localizedDescription))")
        }
    }
}
func scheduleLocalNotification2() {
    // Create Notification Content
    let notificationContent = UNMutableNotificationContent()

    // Configure Notification Content
    notificationContent.title = "Notif 2"
    notificationContent.subtitle = "Local Notifications"
    notificationContent.body = "hellow world + snooze"

    // Set Category Identifier
    notificationContent.categoryIdentifier = Notification.Category.tutorial

    // Add Trigger
    let notificationTrigger = UNTimeIntervalNotificationTrigger(timeInterval: 10.0, repeats: false)

    // Create Notification Request
    let notificationRequest = UNNotificationRequest(identifier: "cocoacasts_local_notification", content: notificationContent, trigger: notificationTrigger)

    // Add Request to User Notification Center
    UNUserNotificationCenter.current().add(notificationRequest) { (error) in
        if let error = error {
            print("Unable to Add Notification Request (\(error), \(error.localizedDescription))")
        }
    }
}

我希望第一个函数创建一个没有任何操作的普通通知,第二个函数创建一个带有操作按钮的通知。所以我创建了以下函数

func configureUserNotificationsCenter() {
    // Configure User Notification Center
    UNUserNotificationCenter.current().delegate = self

    // Define Actions
    let actionReadLater = UNNotificationAction(identifier: Notification.Action.readLater, title: "Read Later", options: [])
    let actionShowDetails = UNNotificationAction(identifier: Notification.Action.showDetails, title: "Show Details", options: [.foreground])
    let actionUnsubscribe = UNNotificationAction(identifier: Notification.Action.unsubscribe, title: "Unsubscribe", options: [.destructive, .authenticationRequired])

    // Define Category
    let tutorialCategory = UNNotificationCategory(identifier: Notification.Category.tutorial, actions: [actionReadLater, actionShowDetails, actionUnsubscribe], intentIdentifiers: [], options: [])

    // Register Category
    UNUserNotificationCenter.current().setNotificationCategories([tutorialCategory])
}

然后我在 viewdidload 中调用 configureUserNotificationsCenter,但这会导致我所有的通知都有这些操作按钮。我只想要使用 scheduleLocalNotifications2 函数安排的通知,而其他函数的通知不应该有这些操作按钮。我该怎么做?

最佳答案

这两种方法都使用 Notification.Category.tutorialnotificationContent.categoryIdentifier。这是您在其上指定按钮的同一类别,这就是它为两者显示的原因。您需要为没有按钮的通知创建第二个类别标识符

关于ios - 两种不同类别的本地通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45080822/

相关文章:

ios - 如何在不更改跟踪 ID 的情况下删除 Google 移动 iOS 应用程序分析中所有以前跟踪的数据

ios - 计算所有 NSManagedObjects 属性的平均值

ios - 安装失败 'cordova-plugin-firebase-authentication' : undefined

iOS 开发中心证书、标识符和配置文件卡在加载屏幕中

swift - 等待回调函数完成 : Swift

iphone - 使用 SBJSON 编写 JSON

ios - 在 iOS 上更新 map 样式时白色闪烁

ios - 使用 MKGeodesicPolyline Swift 3 计算距离将出现错误

objective-c - 无法识别同一 ViewController 中不同 View 上的 TextField

swift - Xcode 表格 View 滚动问题