1 在application:didFinishLaunchingWithOptions:中注册通知时:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { //创建消息上添加的动作,以按钮的形式显示 let acceptAction = UIMutableUserNotificationAction() acceptAction.title = "接受" acceptAction.identifier = "acceptIdentifier" acceptAction.activationMode = UIUserNotificationActivationMode.Foreground let rejectAction = UIMutableUserNotificationAction() rejectAction.title = "拒绝" rejectAction.identifier = "rejectIdentifier" rejectAction.activationMode = UIUserNotificationActivationMode.Background //需要解锁才能处理,如果activationMode = UIUserNotificationActivationMode.Background,则这个属性忽略 rejectAction.authenticationRequired = true rejectAction.destructive = true //创建动作按钮的类别集合 let category = UIMutableUserNotificationCategory() category.identifier = "alert" category.setActions([acceptAction, rejectAction], forContext: UIUserNotificationActionContext.Minimal)
//创建UIUserNotificationSettings,并设置消息的显示类型
let notifSetting = UIUserNotificationSettings(forTypes: .Badge , categories: NSSet(object: category) as? Set<UIUserNotificationCategory>)
//注册本地通知
application.registerUserNotificationSettings(notifSetting)
return true }
2 在使用Push的时候需要在数据包中加入特定的Category字段(字段内容需要前后端定义为一致),终端接收到到后,就能展示上述代码对应Category设置的按钮,和响应按钮事件。
时间: 2024-10-31 16:09:01