正文
本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 推送增加右侧显示图的资料请关注代码网其它相关文章!
发表评论