iOS10富文本推送--NotificationContentExtension

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ZuoWeiXiaoDuZuoZuo/article/details/75314934

NotificationContentExtension文件

根据以下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  @"忽略"  //视频取消按钮标题

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

@interface NotificationViewController () <UNNotificationContentExtension>
@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);
        }
    }

}

猜你喜欢

转载自blog.csdn.net/ZuoWeiXiaoDuZuoZuo/article/details/75314934
今日推荐