title: iOS10富文本推送--UIMutableUserNotificationAction
date: 2017-07-18 15:04:14
tags: 原創分享
AppDelagate文件
添加action
根據以下ContentExtension Info.plist文件中的配置決定category的設置,兩者必須一致
ContentExtension Info.plist
宏定義采用下列代碼:
//推送相關設置
#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;
}