ios - iOS 操作按钮从本地通知中消失,除了最后传入的通知 (NotificationCenter)
问题描述
我正在使用可操作的通知。以下来自后端的数据:
- 通知 ID(字符串)
- 通知标题、正文
- 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
}
请大家帮帮我!!!
解决方案
推荐阅读
- c++ - 避免重复信号订阅的有效方法
- c# - 这个表达式在 GroupBy 方法中是什么意思?
- swift - 无法将 Firebase 存储数据转换为 UIImage
- javascript - 抓取时,console.log 显示数据但未返回
- java - AWS S3 上传图像在部署到 EBS 时不起作用,但在 localhost SpringBoot (JAVA) 中起作用
- reactjs - 使用 jest spyOn 测试库方法
- regex - 如何编写一个正则表达式来捕获大写结束和标题大小写开始的位置?
- matlab - 如何为多个变量添加一个共同的后缀?
- apollo - 如何在 apollo-link-error 中访问 apollo 客户端以重置存储?
- iso8583 - ISO 8583 ALPHA 未转换为 HEX