title: iOS10富文本推送--NotificationContentExtension
date: 2017-07-18 14:46:39
tags: 原創(chuàng)分享
NotificationContentExtension文件
根據(jù)以下ContentExtension Info.plist文件中的配置決定category的設(shè)置,兩者必須一致
ContentExtension Info.plist
宏定義采用下列代碼:
//推送相關(guān)設(shè)置
#define Action_Category_Identifier_Image @"Image_Category" //圖片類別標(biāo)識(shí)符
#define Action_Category_Identifier_Audio @"Audio_Category" //音頻類別標(biāo)識(shí)符
#define Action_Category_Identifier_Movie @"Movie_Category" //視頻類別標(biāo)識(shí)符
#define Action_Identifier_Image_Confirm @"imageConfirmAction" //圖片確認(rèn)按鈕
#define Action_Identifier_Image_Concel @"imageConcelAction" //圖片取消按鈕
#define Action_Identifier_Audio_Confirm @"audioConfirmAction" //音頻確認(rèn)按鈕
#define Action_Identifier_Audio_Concel @"audioConcelAction" //音頻取消按鈕
#define Action_Identifier_Movie_Confirm @"movieConfirmAction" //視頻確認(rèn)按鈕
#define Action_Identifier_Movie_Concel @"movieConcelAction" //視頻取消按鈕
#define Action_Title_Image_Confirm @"查看" //圖片確認(rèn)按鈕標(biāo)題
#define Action_Title_Image_Concel @"忽略" //圖片取消按鈕標(biāo)題
#define Action_Title_Audio_Confirm @"查看" //音頻確認(rèn)按鈕標(biāo)題
#define Action_Title_Audio_Concel @"忽略" //音頻取消按鈕標(biāo)題
#define Action_Title_Movie_Confirm @"查看" //視頻確認(rèn)按鈕標(biāo)題
#define Action_Title_Movie_Concel @"忽略" //視頻取消按鈕標(biāo)題
采用的是自定義布局,注意如果想使用這個(gè)布局的話,
你必須提前在service里面設(shè)置好categoryIdentifier严蓖,它的值是你plist文件里面的任何一個(gè)
@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;
}
響應(yīng)相關(guān)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);
}
}
}