iOS10富文本推送--UIMutableUserNotificationAction

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;
}
阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ZuoWeiXiaoDuZuoZuo/article/details/75314910
个人分类: 分享
上一篇iOS10富文本推送--UNNotificationExtension--基础篇
下一篇iOS10富文本推送--NotificationServiceExtension
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭