我们已经使用通知扩展服务实现了可操作通知。我们收到推送通知,它会显示通知标题、正文、操作按钮的正确内容。我们定义了 3 个类别,比如onebtn、twobtn、threebtn以及每个类别的所有唯一操作。
因此,对于一个通知,一切正常,但是当我们在通知托盘中收到多个通知时,最近的通知操作按钮的文本会替换为所有其他通知的操作按钮的文本。前任。我收到了有关threebtn类别的通知,然后它将向我显示三个按钮,其中包含三个不同的文本,例如现在、稍后和取消(这三个按钮名称将是从推送通知接收到的有效负载的一部分)。现在,我又收到了一个关于 twobtn 类别的通知,其中包含按钮名称、回复并标记为已读并针对该通知显示。
现在的问题是,它将twobtn 类别的按钮名称覆盖到为threebtn 通知接收的通知中。这意味着当我拖动并打开类别 threebtn 的通知时,其按钮名称将是回复、标记为付费和取消 (分配操作时定义的第三个默认文本)。因此,每个通知都会有名称回复并标记为已付款。
尝试过的解决方案:
- 在 NSNotification 下将类别设置为 plist 作为字典(之前我们在运行时添加了类别)
- 分配了唯一的操作和类别名称。
一周以来,我一直被这个问题困扰,但没有找到任何合适的解决方案。请给我一些提示,以便我可以解决这个问题。
谢谢。
class NotificationService: UNNotificationServiceExtension {
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
let (pushNotificationBean, _) = saveNotificationAppGroupDefault(saveNotificationUserInfo: request.content.userInfo)
registerNotificationCategory(pushNotificationBean: pushNotificationBean)
self.contentHandler = contentHandler
bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
}
// MARK: - Register Notificaion Categories and Actions
func registerNotificationCategory(pushNotificationBean: PushNotificationBean) {
// Define Actions
if #available(iOS 10.0, *) {
// Define Action Yes
var singleBtnAction : UNNotificationAction
var trippleBtnYesAction : UNNotificationAction
var trippleBtnNoAction : UNNotificationAction
var trippleBtnPartialAction : UNNotificationAction
var doubleBtnYesAction : UNNotificationAction
var doubleBtNoAction : UNNotificationAction
// Positve Button Text
if let okActionText = pushNotificationBean.actionPositiveText, okActionText.count > 0{
// Single Button Action
singleBtnAction = UNNotificationAction(identifier: self.OkAction,
title: okActionText,
options: .foreground)
// Double Button Action
doubleBtnYesAction = UNNotificationAction(identifier: self.YesAction,
title: okActionText,
options: .foreground)
// Tripple Button Action
trippleBtnYesAction = UNNotificationAction(identifier: self.YesAction,
title: okActionText,
options: .foreground)
}else{
// Single Button Action
singleBtnAction = UNNotificationAction(identifier: self.OkAction,
title: self.OkAction,
options: .foreground)
// Double Button Action
doubleBtnYesAction = UNNotificationAction(identifier: self.YesAction,
title: self.YesAction,
options: .foreground)
// Tripple Button Action
trippleBtnYesAction = UNNotificationAction(identifier: self.YesAction,
title: self.YesAction,
options: .foreground)
}
// Negative Action Text
if let noActionText = pushNotificationBean.actionNegativeText, noActionText.count > 0{
// Double Button Action
doubleBtNoAction = UNNotificationAction(identifier: self.NoAction,
title: noActionText,
options: .foreground)
// Tripple Button Action
trippleBtnNoAction = UNNotificationAction(identifier: self.NoAction,
title: noActionText,
options: .foreground)
} else{
// Double Button Action
doubleBtNoAction = UNNotificationAction(identifier: self.NoAction,
title: self.NoAction,
options: .foreground)
// Tripple Button Action
trippleBtnNoAction = UNNotificationAction(identifier: self.NoAction,
title: self.NoAction,
options: .foreground)
}
// Partial Action Text
if let trippleBtnPartialActionText = pushNotificationBean.actionNeutralText, trippleBtnPartialActionText.count > 0{
// Tripple Button Action
trippleBtnPartialAction = UNNotificationAction(identifier: self.PartialAction,
title: trippleBtnPartialActionText,
options: .foreground)
}else{
// Tripple Button Action
trippleBtnPartialAction = UNNotificationAction(identifier: self.PartialAction,
title: self.PartialAction,
options: .foreground)
}
// Define Single Category
let singleButtonCategory = UNNotificationCategory(identifier: self.SingleButton,
actions: [singleBtnAction],
intentIdentifiers: [self.OkAction],
options: [])
// Define Double Category
let doubleButtonCategory = UNNotificationCategory(identifier: self.DoubleButton,
actions: [doubleBtnYesAction,doubleBtNoAction],
intentIdentifiers: [self.YesAction,self.NoAction],
options: [])
// Define Tripple Category
let tripleButtonCategory = UNNotificationCategory(identifier: self.TripleButton,
actions: [trippleBtnYesAction,trippleBtnNoAction,trippleBtnPartialAction],
intentIdentifiers: [self.YesAction,self.NoAction,self.PartialAction],
options: [])
UNUserNotificationCenter.current().setNotificationCategories([singleButtonCategory,doubleButtonCategory,tripleButtonCategory])
}
}
}