1. 程式人生 > >iOS10富文字推送--NotificationContentExtension

iOS10富文字推送--NotificationContentExtension

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

}