Ios 在运行时更改交互式通知中的操作标题

Ios 在运行时更改交互式通知中的操作标题,ios,interactive,Ios,Interactive,根据我的理解,我必须在其中注册本地或远程交互通知的类别和操作,现在我的要求是显示按钮,动态标题作为推送负载的一部分 作为替代方案,我还尝试了在收到远程通知时使用注册的设置触发本地通知的选项,但不知何故,该选项不起作用,并且本地通知未被触发。非常感谢您的帮助。简而言之,您不能在运行时更改操作按钮标题 但是,您可以尝试以下方法: 在向设备发送通知之前,请使用可用内容属性向其发送“无声”通知。在此通知中,发送表示新操作按钮标题的数据 使用注册表通知设置更新相关类别: 发送带有您刚刚创建的新动作标题的“

根据我的理解,我必须在其中注册本地或远程交互通知的类别和操作,现在我的要求是显示按钮,动态标题作为推送负载的一部分


作为替代方案,我还尝试了在收到远程通知时使用注册的设置触发本地通知的选项,但不知何故,该选项不起作用,并且本地通知未被触发。非常感谢您的帮助。

简而言之,您不能在运行时更改操作按钮标题

但是,您可以尝试以下方法:

  • 在向设备发送通知之前,请使用
    可用内容
    属性向其发送“无声”通知。在此通知中,发送表示新操作按钮标题的数据

  • 使用注册表通知设置更新相关类别:

  • 发送带有您刚刚创建的新动作标题的“真实”通知


  • 它需要测试,因为我从未在生产模式中尝试过。另外,请考虑到,您将要将通知发送量翻倍。

    正如@Asaf所述,需要使用registerUserNotificationSettings在相关类别中重新注册:

    只需使用以下代码即可实现此目的:

     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)
    

    //用于通知操作-对象
    结构UserNotificationAction{
    变量标题:字符串
    变量标识符:字符串
    var activationMode:UIUserNotificationActivationMode
    需要var身份验证:Bool
    var是破坏性的:Bool
    初始化(标题:字符串,标识符:字符串,激活模式:UIUserNotificationActivationMode?=.Background,authenticationRequired:Bool?=false,isDestructive:Bool?=false){
    self.title=标题
    self.identifier=标识符
    self.activationMode=activationMode!
    self.authenticationRequired=authenticationRequired!
    self.isDestructive=isDestructive!
    }
    }
    //用于创建动态UIMutableUserNotificationCategory和UIMutableUserNotificationAction的函数
    func createCategoryUserNotification(notificationActions:[UserNotificationAction])->UIMutableUserNotificationCategory{
    var UserNotificationActions=[UIMutableUserNotificationAction]()
    对于notificationActions中的userAction{
    让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{
    让userCategory=UIMutableUserNotificationCategory()
    userCategory.identifier=getGenericCategoryIdentifier(notificationActions)
    userCategory.setActions(UserNotificationActions,forContext:.默认值)
    返回用户类别
    }
    返回runTimeCategoryUserNotification
    }
    //重新注册用户通知(如果需要任何新类别)
    func RegisterModifiedUserNotificationCategory(notificationActions:[UserNotificationAction])->Bool{
    //让UwerNotification=UserNotificationActions()
    让newCategory=createCategoryUserNotification(notificationActions)
    let settings=UIApplication.sharedApplication().currentUserNotificationSettings()
    如果设置!.types==.None{
    返回错误
    }
    让oldUserNotificationsettings=UIApplication.sharedApplication().currentUserNotificationSettings()
    var newCategories:[UIMutableUserNotificationCategory]=[UIMutableUserNotificationCategory]()
    var isNewCategoryFoundinList:Bool=false
    用于(oldUserNotificationsettings?.categories)中的类别{
    如果category.identifier==newCategory.identifier{
    isNewCategoryFoundInExist=true
    }
    if category.identifier==kGeneralCategoryUserNotification{//如果有预定义的类别
    newCategories.append(generalCategoryUserNotification)
    }else if category.identifier==kCallCategoryUserNotification{//如果有预定义的类别
    newCategories.append(CallCategoryUserNotification)
    }/*else{//在运行时注册了某个XYZ类别,如果仍要重新注册
    让actions=category.actionsForContext(.Default)
    打印(类别.操作或上下文(.Default))
    如果操作?.count>0{
    var GenericCategoryUserNotification:UIMutableUserNotificationCategory{
    让userCategory=UIMutableUserNotificationCategory()
    userCategory.identifier=UwerNotification.GetGenericCategoryIdentifierFromUserNotiAction(操作!)
    userCategory.setActions(操作,forContext:.默认值)
    返回用户类别
    }
    newCategories.append(GenericCategorityUserNotification)
    }
    }*/
    }
    //注册新类别的通知(如有)
    如果!NewCategoryFoundinList{
    newCategories.append(newCategories)
    var categories=Set()
    对于新类别中的categr{
    类别。插入(类别)
    }
    let settings=UIUserNotificationSettings(FORTYPE:[.Alert、.Badge、.Sound],类别:类别)
    //在此处注册registerUserNotificationSettings
    }
    返回真值
    }
    //基于动作标识符集合的泛型类别标识符
    func getGenericCategoryIdentifier(notificationActions:[UserNotificationAction])->String{
    让我们行动起来
    
    //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
        } 
    
    - (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"
          }
        ]
        }
     }