欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

iOS10富文本推送--UIMutableUserNotificationAction

程序员文章站 2023-01-11 20:19:20
appdelagate文件 添加action 根据以下contentextension info.plist文件中的配置决定category的设置,两者必须一致 宏定义采用下列代码: //推送...

appdelagate文件

添加action

根据以下contentextension info.plist文件中的配置决定category的设置,两者必须一致
iOS10富文本推送--UIMutableUserNotificationAction

宏定义采用下列代码:

//推送相关设置
#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 *)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;
}