title: iOS10富文本推送--UIMutableUserNotificationAction
date: 2017-07-18 15:04:14
tags: 原创分享
AppDelagate文件
添加action
根据以下ContentExtension Info.plist文件中的配置决定category的设置,两者必须一致
宏定义采用下列代码:
//推送相关设置
#define Action_Category_Identifier_Image @"Image_Category" //图片类别标识符
#define Action_Category_Identifier_Audio @"Audio_Category" //音频类别标识符
#define Action_Category_Identifier_Movie @"Movie_Category" //视频类别标识符
#define Action_Identifier_Image_Confirm @"imageConfirmAction" //图片确认按钮
#define Action_Identifier_Image_Concel @"imageConcelAction" //图片取消按钮
#define Action_Identifier_Audio_Confirm @"audioConfirmAction" //音频确认按钮
#define Action_Identifier_Audio_Concel @"audioConcelAction" //音频取消按钮
#define Action_Identifier_Movie_Confirm @"movieConfirmAction" //视频确认按钮
#define Action_Identifier_Movie_Concel @"movieConcelAction" //视频取消按钮
#define Action_Title_Image_Confirm @"查看" //图片确认按钮标题
#define Action_Title_Image_Concel @"忽略" //图片取消按钮标题
#define Action_Title_Audio_Confirm @"查看" //音频确认按钮标题
#define Action_Title_Audio_Concel @"忽略" //音频取消按钮标题
#define Action_Title_Movie_Confirm @"查看" //视频确认按钮标题
#define Action_Title_Movie_Concel @"忽略" //视频取消按钮标题
添加相应类别的aciton,一个类别必须对应一个category,
在下面这个方法里面执行,
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
//添加相应类别的aciton,一个类别必须对应一个category
- (void)addNotificationAction{
//Image_Category
UIMutableUserNotificationAction *imageConfirmAction = [self creatNotificationActionIdentifier:Action_Identifier_Image_Confirm
title:Action_Title_Image_Confirm
activationMode:UIUserNotificationActivationModeForeground];
imageConfirmAction.authenticationRequired = YES;
imageConfirmAction.destructive = YES;
UIMutableUserNotificationAction *imageConcelAction = [self creatNotificationActionIdentifier:Action_Identifier_Image_Concel
title:Action_Title_Image_Concel
activationMode:UIUserNotificationActivationModeBackground];
UIMutableUserNotificationCategory *ImageCategory = [self creatNotificationCategoryIdentifier:Action_Category_Identifier_Image
setActions:@[imageConfirmAction,imageConcelAction]
forContext:UIUserNotificationActionContextDefault];
//Audio_Category
UIMutableUserNotificationAction *audioConfirmAction = [self creatNotificationActionIdentifier:Action_Identifier_Audio_Confirm
title:Action_Title_Audio_Confirm
activationMode:UIUserNotificationActivationModeForeground];
audioConfirmAction.authenticationRequired = YES;
audioConfirmAction.destructive = YES;
UIMutableUserNotificationAction *audioConcelAction = [self creatNotificationActionIdentifier:Action_Identifier_Audio_Concel
title:Action_Title_Audio_Concel
activationMode:UIUserNotificationActivationModeBackground];
UIMutableUserNotificationCategory *audioCategory = [self creatNotificationCategoryIdentifier:Action_Category_Identifier_Audio
setActions:@[audioConfirmAction,audioConcelAction]
forContext:UIUserNotificationActionContextDefault];
//Movie_Category
UIMutableUserNotificationAction *movieConfirmAction = [self creatNotificationActionIdentifier:Action_Identifier_Movie_Confirm
title:Action_Title_Movie_Confirm
activationMode:UIUserNotificationActivationModeForeground];
movieConfirmAction.authenticationRequired = YES;
movieConfirmAction.destructive = YES;
UIMutableUserNotificationAction *movieConcelAction = [self creatNotificationActionIdentifier:Action_Identifier_Movie_Concel
title:Action_Title_Movie_Concel
activationMode:UIUserNotificationActivationModeBackground];
UIMutableUserNotificationCategory *movieCategory = [self creatNotificationCategoryIdentifier:Action_Category_Identifier_Movie
setActions:@[movieConfirmAction,movieConcelAction]
forContext:UIUserNotificationActionContextDefault];
NSSet *categories = [NSSet setWithObjects:ImageCategory,audioCategory,movieCategory,nil];
UIUserNotificationType types = (UIUserNotificationTypeAlert|
UIUserNotificationTypeSound|
UIUserNotificationTypeBadge);
UIUserNotificationSettings *settings;
settings = [UIUserNotificationSettings settingsForTypes:types
categories:categories];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
}
创建一个category
//创建一个category
- (UIMutableUserNotificationCategory*)creatNotificationCategoryIdentifier:(NSString *)identifier
setActions:(nullable NSArray<UIUserNotificationAction *> *)actions
forContext:(UIUserNotificationActionContext)context
{
UIMutableUserNotificationCategory *category = [[UIMutableUserNotificationCategory alloc] init];
category.identifier = identifier;//这组动作的唯一标示
[category setActions:actions forContext:context];
return category;
}
创建一个action
//创建一个action
-(UIMutableUserNotificationAction *)creatNotificationActionIdentifier:(NSString *)identifier
title:(NSString *)title
activationMode:(UIUserNotificationActivationMode)activationMode
{
UIMutableUserNotificationAction *action = [[UIMutableUserNotificationAction alloc] init]; //第二按钮
action.identifier = identifier;
action.title = title;
action.activationMode = activationMode;
return action;
}