iOS10富文字推送--UIMutableUserNotificationAction
阿新 • • 發佈:2018-11-11
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;
}