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

iOS10富文本推送--NotificationContentExtension

程序员文章站 2024-01-13 23:03:04
notificationcontentextension文件 根据以下contentextension info.plist文件中的配置决定category的设置,两者必须一致 宏定义采用下列代码...

notificationcontentextension文件

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

宏定义采用下列代码:

//推送相关设置
#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  @"忽略"  //视频取消按钮标题

采用的是自定义布局,注意如果想使用这个布局的话,
你必须提前在service里面设置好categoryidentifier,它的值是你plist文件里面的任何一个

@interface notificationviewcontroller () 
@property (nonatomic, strong)uiimageview *imageview;
@property (nonatomic,strong)uilabel *label;
@end

lazyload

-(uiimageview *)imageview{
    if (_imageview == nil) {
        _imageview = [[uiimageview alloc] init];
        _imageview.contentmode = uiviewcontentmodescaleaspectfit;
    }
    return _imageview;
}

addview

- (void)viewdidload {
    [super viewdidload];
    [self.view addsubview:self.imageview];
    // do any required interface initialization here.
}

取出多媒体资料并展示到视图上,下面为image

- (void)didreceivenotification:(unnotification *)notification {
    nslog(@"notification.request.content.userinfo%@",notification.request.content.userinfo);
    unnotificationcontent * content = notification.request.content;
    cgfloat widthtime = 2;
    if ([uiscreen mainscreen].bounds.size.width>375) {
        widthtime = 3.0;
    }
    uiimage *image = nil;
    if (content.attachments.count) {
        unnotificationattachment * attachment_img = content.attachments[0];
        if (attachment_img.url.startaccessingsecurityscopedresource) {
            image = [uiimage imagewithcontentsoffile:attachment_img.url.path];
            self.imageview.image = image;
        }
    }
    self.imageview.frame = self.view.frame;
    self.label.text = notification.request.content.body;
}

响应相关action

-(void)didreceivenotificationresponse:(unnotificationresponse *)response completionhandler:(void (^)(unnotificationcontentextensionresponseoption))completion{
    unnotificationcontent *content = [response.notification.request.content mutablecopy];
    nsstring *category =  content.categoryidentifier;
    nsstring *actionidentifier = [response.actionidentifier copy];
   if ([category isequaltostring:action_category_identifier_image]) {
        if ([actionidentifier isequaltostring:action_identifier_image_confirm]) {
            completion(unnotificationcontentextensionresponseoptiondismissandforwardaction);
        }else{
            completion(unnotificationcontentextensionresponseoptiondismiss);
        }
    }else if ([category isequaltostring:action_category_identifier_audio]){
        if ([actionidentifier isequaltostring:action_identifier_audio_confirm]) {
            completion(unnotificationcontentextensionresponseoptiondismissandforwardaction);
        }else{
            completion(unnotificationcontentextensionresponseoptiondismiss);
        }
    }else{
        if ([actionidentifier isequaltostring:action_identifier_movie_confirm]) {
            completion(unnotificationcontentextensionresponseoptiondismissandforwardaction);
        }else{
            completion(unnotificationcontentextensionresponseoptiondismiss);
        }
    }

}