iOS推送增加右侧显示图Service Extension
(福利推荐:你还在原价购买阿里云服务器?现在阿里云0.8折限时抢购活动来啦!4核8G企业云服务器仅2998元/3年,立即抢购>>>:9i0i.cn/aliyun)
正文
本Demo推送使用的是极光推送(换成其他推送改动也不大)极光文档 极光Demo
先看下效果图,在系统的推送弹窗右侧增加了一个图片
工程配置(一)
- 首先需要一个已经集成了极光推送,并且可以正常接收推送的工程(参考极光文档again);
- 配置主Target,如下截图所示,勾选主Target的Background Modes;
- 创建Service Extension,看下面的三图;
- 给创建好的PushExtension(子Target)配置Push Notifications,这一步操作就和主Target的配置推送一样;
工程配置(二)集成JPushExtension
这一步是按照需求可选的,引入JPushExtension的目的是为了极光推送做统计
处理推送显示的内容
这是配置好的工程目录,多了一个PushExtention
文件夹
NotificationService.m
文件的内容改为如下
#import "NotificationService.h" #import "JPushNotificationExtensionService.h" @interface NotificationService () @property (nonatomic, strong) void (^contentHandler)(UNNotificationContent *contentToDeliver); @property (nonatomic, strong) UNMutableNotificationContent *bestAttemptContent; @end @implementation NotificationService - (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler { self.contentHandler = contentHandler; self.bestAttemptContent = [request.content mutableCopy]; // 读取图片地址,并加载 NSString *imgUrl = [NSString stringWithFormat:@"%@", self.bestAttemptContent.userInfo[@"imageUrl"]]; // ??图片字段的key值需要跟后台开发统一 if (imgUrl) { NSURL *fileURL = [NSURL URLWithString:imgUrl]; [self downloadAndSave:fileURL handler:^(NSString *localPath) { if (localPath) { UNNotificationAttachment * attachment = [UNNotificationAttachment attachmentWithIdentifier:@"myAttachment" URL:[NSURL fileURLWithPath:localPath] options:nil error:nil]; self.bestAttemptContent.attachments = @[attachment]; } [self apnsDeliverWith:request]; }]; } else { [self apnsDeliverWith:request]; } } - (void)serviceExtensionTimeWillExpire { self.contentHandler(self.bestAttemptContent); } #pragma mark - 私有方法 - (void)downloadAndSave:(NSURL *)fileURL handler:(void (^)(NSString *))handler { // 这里需要用系统网络请求来下载图片 NSURLSession *session = [NSURLSession sharedSession]; NSURLSessionDownloadTask *task = [session downloadTaskWithURL:fileURL completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) { NSString *localPath = nil; if (!error) { // 临时文件夹路径,APP没有运行时会自动清除图片,不会占用内存 NSString *localURL = [NSString stringWithFormat:@"%@/%@", NSTemporaryDirectory(), fileURL.lastPathComponent]; if ([[NSFileManager defaultManager] moveItemAtPath:location.path toPath:localURL error:nil]) { localPath = localURL; } } handler(localPath); }]; [task resume]; } - (void)apnsDeliverWith:(UNNotificationRequest *)request { [JPushNotificationExtensionService jpushSetAppkey:@"本应用在极光平台的AppKey"]; [JPushNotificationExtensionService jpushReceiveNotificationRequest:request with:^ { NSLog(@"apns upload success"); self.contentHandler(self.bestAttemptContent); }]; } @end
注意事项
如果传了图片地址却还不显示,不要惊慌,先请确保图片别太大,而且可以使用NSURLSession下载,否则就会出现图片不显示的问题。
以上就是iOS推送增加右侧显示图Service Extension的详细内容,更多关于iOS 推送增加右侧显示图的资料请关注程序员之家其它相关文章!
相关文章
Android中getActivity()为null的解决办法
在Android开发的时候可能遇过出现getActivity()出现null的时候导致程序报出空指针异常,那么遇到这种情况改如何解决,下面跟着小编一起去看看。2016-08-08IOS中(assign,retain,copy,weak,strong)的区别以及nonatomic的含义
这篇文章主要介绍了我们在声明@property 属性时,总是要在括号中写上assign、retain、copy、weak、strong中的一个,他们的区别,需要的朋友可以参考下2017-03-03
最新评论