iOS--富文本推送UIMutableUserNotificationAction


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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

卟败灬筱龙

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值