iOS10富文本推送--UIMutableUserNotificationAction


title: iOS10富文本推送--UIMutableUserNotificationAction
date: 2017-07-18 15:04:14
tags: 原創(chuàng)分享


AppDelagate文件

添加action

根據(jù)以下ContentExtension Info.plist文件中的配置決定category的設(shè)置,兩者必須一致


ContentExtension Info.plist

宏定義采用下列代碼:

//推送相關(guān)設(shè)置
#define Action_Category_Identifier_Image @"Image_Category" //圖片類別標(biāo)識(shí)符
#define Action_Category_Identifier_Audio @"Audio_Category" //音頻類別標(biāo)識(shí)符
#define Action_Category_Identifier_Movie @"Movie_Category" //視頻類別標(biāo)識(shí)符
#define Action_Identifier_Image_Confirm @"imageConfirmAction"  //圖片確認(rèn)按鈕
#define Action_Identifier_Image_Concel  @"imageConcelAction"   //圖片取消按鈕
#define Action_Identifier_Audio_Confirm @"audioConfirmAction"  //音頻確認(rèn)按鈕
#define Action_Identifier_Audio_Concel  @"audioConcelAction"   //音頻取消按鈕
#define Action_Identifier_Movie_Confirm @"movieConfirmAction"  //視頻確認(rèn)按鈕
#define Action_Identifier_Movie_Concel  @"movieConcelAction"   //視頻取消按鈕
#define Action_Title_Image_Confirm @"查看"  //圖片確認(rèn)按鈕標(biāo)題
#define Action_Title_Image_Concel  @"忽略"  //圖片取消按鈕標(biāo)題
#define Action_Title_Audio_Confirm @"查看"  //音頻確認(rèn)按鈕標(biāo)題
#define Action_Title_Audio_Concel  @"忽略"  //音頻取消按鈕標(biāo)題
#define Action_Title_Movie_Confirm @"查看"  //視頻確認(rèn)按鈕標(biāo)題
#define Action_Title_Movie_Concel  @"忽略"  //視頻取消按鈕標(biāo)題

添加相應(yīng)類別的aciton,一個(gè)類別必須對應(yīng)一個(gè)category,
在下面這個(gè)方法里面執(zhí)行,

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
//添加相應(yīng)類別的aciton,一個(gè)類別必須對應(yīng)一個(gè)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]; 
}

創(chuàng)建一個(gè)category

//創(chuàng)建一個(gè)category
- (UIMutableUserNotificationCategory*)creatNotificationCategoryIdentifier:(NSString *)identifier
                                                               setActions:(nullable NSArray<UIUserNotificationAction *> *)actions
                                                               forContext:(UIUserNotificationActionContext)context
{
    UIMutableUserNotificationCategory *category = [[UIMutableUserNotificationCategory alloc] init];
    category.identifier = identifier;//這組動(dòng)作的唯一標(biāo)示
    [category setActions:actions forContext:context];
    return category;
}

創(chuàng)建一個(gè)action

//創(chuàng)建一個(gè)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;
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容