首页 > 解决方案 > iOS 操作按钮从本地通知中消失,除了最后传入的通知 (NotificationCenter)

问题描述

我正在使用可操作的通知。以下来自后端的数据:

  1. 通知 ID(字符串)
  2. 通知标题、正文
  3. 2个动作按钮每次都有不同的标题

所以,我用唯一的notificationId 注册了UNNotificationCategory 和categoryIdentifier(对于UNMutableNotificationContent)。

问题陈述:- 当然,所有传入通知都会弹出其通知操作。但是当我在NotificationCenter中检查这些通知的操作按钮时。只有最后一个通知显示他的操作按钮和其他显示没有按钮的通知。

为每个传入通知执行以下代码片段:-

这就是我注册通知操作和通知类型的方式

struct NotificationStruct {
    struct Action {
        static let positive = "positive" 
        static let negative = "negative"
    }
}


//Registering notification actions with notificationCatagory
//This function called each time for incoming notification
func registerCatagory ()
{
    let actionPositive = UNNotificationAction (
        identifier : NotificationStruct.Action.positive,
        title : buttonPositiveLabel,
        options : []
    )
    let actionNegative = UNNotificationAction (
        identifier : NotificationStruct.Action.negative,
        title : buttonNegativeLabel,
        options : []
    )
    let notificationCategory = UNNotificationCategory (
        identifier : notificationId,
        actions : [actionPositive, actionNegative],
        intentIdentifiers : [],
        options : []
    )
    UNUserNotificationCenter.current ()
    .setNotificationCategories ([notificationCategory])
}

这就是我注册通知类别标识符以触发通知的方式:

   func triggerNotificaion ()
    {
        let content = UNMutableNotificationContent ()
        content.title = title 
        content.body = body 
        content.categoryIdentifier = notificationId 
        let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 2, repeats: false)
        let request = UNNotificationRequest (
            identifier : String (getRandomId()),
            content : content,
            trigger : trigger
        )
        //scheduling the notification
        UNUserNotificationCenter.current ()
        .add (request, withCompletionHandler: nil)
    }

    func getRandomId () -> Int {
    let randomInt = Int.random (in: 0..<999999)
    return randomInt
    }

请大家帮帮我!!!

标签: iosswiftunusernotificationcenteractionable-notification

解决方案


推荐阅读