根据我的理解,我必须在本地或远程交互式通知中注册其中的类别和操作,现在我的要求是我想显示带有动态标题的按钮,作为推送有效载荷的一部分。
作为替代方案,我还尝试了在收到远程通知时注册的设置的本地通知的选项,但是它以某种方式不起作用,并且本地通知未触发。帮助高度赞赏。
简短的答案是您无法在运行时更改操作按钮标题。
但是,您可以尝试以下内容:
-
在向设备发送通知之前,请使用
content-available
属性发送"无声"通知。在此通知中,发送一个表示您的新操作按钮标题的数据。 -
使用
registerUserNotificationSettings:
。
更新相关类别 用新的动作标题发送"真实"通知,您刚刚创建了。
它需要进行测试,因为我从未在生产模式下尝试过。另外,考虑到您将要加倍您的通知金额。
对于寻找这个问题解决方案的人
1.创建通知服务扩展
- 在didReceivenotificationRequest :( unotificationRequest *)请求contenthandler :( void(^)(unotification content * _nonnull))contentHandler
创建操作按钮并再次注册到通知中心
for ex:
- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler {
NSDictionary * userInfo = request.content.userInfo;
NSArray * actionButtons = [userInfo valueForKeyPath:@"payload.actionButton"];
if(actionButtons){
NSMutableArray * buttons = [NSMutableArray new];
for(NSDictionary * actionButton in actionButtons){
UNNotificationAction *ActionBtn = [UNNotificationAction actionWithIdentifier:[actionButton valueForKey:@"actionDeeplink"] title:[actionButton valueForKey:@"actionName"] options:UNNotificationActionOptionNone];
[buttons addObject:ActionBtn];
}
UNNotificationCategory *category = [UNNotificationCategory categoryWithIdentifier:@"myNotificationCategory" actions:buttons intentIdentifiers:@[] options:UNNotificationCategoryOptionNone];
NSSet *categories = [NSSet setWithObject:category];
[[UNUserNotificationCenter currentNotificationCenter] setNotificationCategories:categories];
}
}
然后在您的通知有效载荷中执行类似的操作:
{
"aps" : {
"alert" : {
"title" : "Manish Malviya",
"body" : "iOS Developer"
},
"mutable-content" : 1,
"content-available" : 1,
"category" : "myNotificationCategory"
},
"payload" : {"actionButton": [
{
"actionName": "Yes",
"actionDeeplink": "nottest"
},
{
"actionName": "May be",
"actionDeeplink": "tes"
},
{
"actionName": "No",
"actionDeeplink": "test"
}
]
}
}
和voila请参阅输出。
正如@ASAF所述,需要使用registerUsernotificationettings重新注册相关类别:
只需使用以下代码实现这一目标:
let actionAnswer = UserNotificationAction(title: kAnswerEventTitle, identifier: kAnswerEventIdentitfier, activationMode: .Foreground, authenticationRequired: false, isDestructive: false)
let actionReject = UserNotificationAction(title: kRejectEventTitle, identifier: kRejectEventIdentitfier, activationMode: .Background, authenticationRequired: false, isDestructive: true)
registerModiifedUserNotificationCategory(userActions)
//For Notification Actions - object
struct UserNotificationAction {
var title: String
var identifier: String
var activationMode: UIUserNotificationActivationMode
var authenticationRequired: Bool
var isDestructive:Bool
init(title: String, identifier: String, activationMode: UIUserNotificationActivationMode? = .Background, authenticationRequired: Bool? = false, isDestructive: Bool? = false){
self.title = title
self.identifier = identifier
self.activationMode = activationMode!
self.authenticationRequired = authenticationRequired!
self.isDestructive = isDestructive!
}
}
//func for dynamic UIMutableUserNotificationCategory and UIMutableUserNotificationAction creation
func createCategoryUserNotification(notificationActions: [UserNotificationAction]) -> UIMutableUserNotificationCategory {
var UserNotificationActions = [UIMutableUserNotificationAction]()
for userAction in notificationActions {
let actionItem = UIMutableUserNotificationAction()
actionItem.activationMode = userAction.activationMode
actionItem.title = userAction.title
actionItem.identifier = userAction.identifier
actionItem.destructive = userAction.isDestructive
actionItem.authenticationRequired = userAction.authenticationRequired
UserNotificationActions.append(actionItem)
}
var runTimeCategoryUserNotification: UIMutableUserNotificationCategory{
let userCategory = UIMutableUserNotificationCategory()
userCategory.identifier = getGenericCategoryIdentifier(notificationActions)
userCategory.setActions(UserNotificationActions, forContext:.Default)
return userCategory
}
return runTimeCategoryUserNotification
}
//Re-Registering USer notification, if any new Categories required
func registerModiifedUserNotificationCategory( notificationActions: [UserNotificationAction])-> Bool {
// let UwerNotification = UserNotificationActions()
let newCategory = createCategoryUserNotification(notificationActions)
let settings = UIApplication.sharedApplication().currentUserNotificationSettings()
if settings!.types == .None {
return false
}
let oldUserNotificationsettings = UIApplication.sharedApplication().currentUserNotificationSettings()
var newCategories:[UIMutableUserNotificationCategory] = [UIMutableUserNotificationCategory]()
var isNewCategoryFoundInExist: Bool = false
for category in (oldUserNotificationsettings?.categories)! {
if category.identifier == newCategory.identifier{
isNewCategoryFoundInExist = true
}
if category.identifier == kGeneralCategoryUserNotification{//if any predefined Categories
newCategories.append(generalCategoryUserNotification)
}else if category.identifier == kCallCategoryUserNotification{//if any predefined Categories
newCategories.append(callCategoryUserNotififation)
}/* else{// some XYZ Category registered at runtime and if still want to Re-register
let actions = category.actionsForContext(.Default)
print(category.actionsForContext(.Default))
if actions?.count > 0 {
var genericCategoryUserNotififation: UIMutableUserNotificationCategory{
let userCategory = UIMutableUserNotificationCategory()
userCategory.identifier = UwerNotification.getGenericCategoryIdentifierFromUserNotifAction(actions!)
userCategory.setActions(actions, forContext:.Default)
return userCategory
}
newCategories.append(genericCategoryUserNotififation )
}
}*/
}
//REgister with new category of Notification if any
if !isNewCategoryFoundInExist {
newCategories.append(newCategory)
var categories = Set<UIUserNotificationCategory>()
for categr in newCategories{
categories.insert(categr)
}
let settings = UIUserNotificationSettings(forTypes:[.Alert, .Badge, .Sound], categories: categories)
//Register here registerUserNotificationSettings
}
return true
}
// generic Category Identifer based on concat of Action Identifiers
func getGenericCategoryIdentifier(notificationActions: [UserNotificationAction]) -> String {
let actionIdentifiers = notificationActions.map({$0.identifier})
let genericCategoryIdentifier = actionIdentifiers.joinWithSeparator("")
return genericCategoryIdentifier
}