关于消息推送,网路上的博客已经讲得非常详尽,最近研究一个自定义消息推送界面的需求,是iOS 10以后才有的新特性,但目前资料也不太多,于是自己整理了下,发出来以供来者参考。
先奉上本篇Demo源码:JZNotic - chenzht
前戏需要自己集成好普通的消息推送功能,这里不再繁述,以下都是建立在消息推送已经成功搭建的基础上,正文kai'shi。
- 点击File--New--Target
- 选择Notification Content
- 会生成这样一个文件夹,这就一目了然,和开发一个普通界面并无二致。但需要注意的是,在MainInterface.storyboard中自定你的UI页面,可以随意发挥,但是这个UI界面只能用于展示,并不能响应点击或者手势其他事件,只能通过category来实现。
- 说起这个category,便要在3中的info.plist进行如下设置,UNNotificationExtensionCategory需设置与推送消息的category一致,UNNotificationExtensionDefaultContentHidden默认值便是0
- 此后tagert选择自己创建的Notification Content -> Run -> 在弹出的选款中选择Demo然后运行
- 效果如下(Demo中选择自定义1,然后点击start即可看到效果),左边是推送消息里解析的图片,右边是AVPlayer播放的视频,下面则是自定义添加的Action。
分别说说这几部分代码。
1> Custom Content 用户自定义界面
NotificationViewController.m
#import "NotificationViewController.h"
#import <UserNotifications/UserNotifications.h>
#import <UserNotificationsUI/UserNotificationsUI.h>
#import <AVFoundation/AVFoundation.h>
@interface NotificationViewController () <UNNotificationContentExtension>
@property (weak, nonatomic) IBOutlet UIView *player_view;
@property IBOutlet UILabel *label;
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@end
@implementation NotificationViewController
{
AVPlayer *player;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any required interface initialization here.
}
- (void)didReceiveNotification:(UNNotification *)notification {
self.label.text = notification.request.content.body;
UNNotificationContent * content = notification.request.content;
UNNotificationAttachment * attachment_img = content.attachments[0];
if (attachment_img.URL.startAccessingSecurityScopedResource) {
self.imageView.image = [UIImage imageWithContentsOfFile:attachment_img.URL.path];
}
// 视频
UNNotificationAttachment * attachment_mp4 = content.attachments[1];
AVPlayerItem *item = [AVPlayerItem playerItemWithURL:attachment_mp4.URL];
player = [AVPlayer playerWithPlayerItem:item];
AVPlayerLayer *layer = [AVPlayerLayer playerLayerWithPlayer:player];
layer.frame = CGRectMake(0, 0, self.player_view.frame.size.width, self.player_view.frame.size.height);
// 显示播放视频的视图层要添加到self.view的视图层上面
[self.player_view.layer addSublayer:layer];
//最后一步开始播放
[player play];
}
@end
2> Default Content,该部分是可以隐藏的,只要在将Content的Info。plist文件中UNNotificationExtensionDefaultContentHidden设置为YES即可
Paste_Image.png3> Notification Actions部分,addNotificationAction是上图效果,addNotificationAction2会将效果图中的Notification Actions变为输入框
JZUserNotification.m
#import "JZUserNotification.h"
#import <UserNotifications/UserNotifications.h>
#import <CoreLocation/CoreLocation.h>
#import "AppDelegate.h"
#import "DownloadManager.h"
+ (void)addNotificationAction{
// UNNotificationActionOptions
// * 需要解锁显示,点击不会进app。
// UNNotificationActionOptionAuthenticationRequired = (1 << 0),
// * 红色文字。点击不会进app。
// UNNotificationActionOptionDestructive = (1 << 1),
// * 黑色文字。点击会进app。
// UNNotificationActionOptionForeground = (1 << 2),
//创建按钮Action
UNNotificationAction *lookAction = [UNNotificationAction actionWithIdentifier:@"action.join" title:@"接收邀请" options:UNNotificationActionOptionAuthenticationRequired];
UNNotificationAction *joinAction = [UNNotificationAction actionWithIdentifier:@"action.look" title:@"查看邀请" options:UNNotificationActionOptionForeground];
UNNotificationAction *cancelAction = [UNNotificationAction actionWithIdentifier:@"action.cancel" title:@"取消" options:UNNotificationActionOptionDestructive];
// 注册 category
// * identifier 标识符
// * actions 操作数组
// * intentIdentifiers 意图标识符 可在 <Intents/INIntentIdentifiers.h> 中查看,主要是针对电话、carplay 等开放的 API。
// * options 通知选项 枚举类型 也是为了支持 carplay
UNNotificationCategory *notificationCategory = [UNNotificationCategory categoryWithIdentifier:@"JZCategory" actions:@[lookAction, joinAction, cancelAction] intentIdentifiers:@[] options:UNNotificationCategoryOptionCustomDismissAction];
// 将 category 添加到通知中心
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center setNotificationCategories:[NSSet setWithObject:notificationCategory]];
}
+ (void)addNotificationAction2{
// 创建 UNTextInputNotificationAction 比 UNNotificationAction 多了两个参数
// * buttonTitle 输入框右边的按钮标题
// * placeholder 输入框占位符
UNTextInputNotificationAction *inputAction = [UNTextInputNotificationAction actionWithIdentifier:@"action.input" title:@"输入" options:UNNotificationActionOptionForeground textInputButtonTitle:@"发送" textInputPlaceholder:@"tell me loudly"];
// 注册 category
UNNotificationCategory *notificationCategory = [UNNotificationCategory categoryWithIdentifier:@"JZCategory" actions:@[inputAction] intentIdentifiers:@[] options:UNNotificationCategoryOptionCustomDismissAction];
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center setNotificationCategories:[NSSet setWithObject:notificationCategory]];
}
Action事件的操作:所有的push(不管远端或者本地)点击都会走到下面的代理方法(如果在Custom Content中添加Button,点击事件不会由NotificationViewController来处理,而是同样走这个回调)
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler
{
NSLog(@"didReceiveNotificationResponse : %@", response);
//点击或输入action
NSString* actionIdentifierStr = response.actionIdentifier;
//输入
if ([response isKindOfClass:[UNTextInputNotificationResponse class]]) {
NSString* userSayStr = [(UNTextInputNotificationResponse *)response userText];
NSLog(@"actionid = %@\n userSayStr = %@",actionIdentifierStr, userSayStr);
// User Defined
}
//点击
if ([actionIdentifierStr isEqualToString:@"action.join"]) {
// User Defined
NSLog(@"actionid = %@\n",actionIdentifierStr);
}else if ([actionIdentifierStr isEqualToString:@"action.look"]){
// User Defined
NSLog(@"actionid = %@\n",actionIdentifierStr);
}
completionHandler();
}
网友评论