Error message here!

Hide Error message here!

忘记密码?

Error message here!

请输入正确邮箱

Hide Error message here!

密码丢失?请输入您的电子邮件地址。您将收到一个重设密码链接。

Error message here!

返回登录

Close

通知实战 设置通知图片(iOS10以后的)

甘林梦 2019-02-24 20:59:00 阅读数:225 评论数:0 点赞数:0 收藏数:0

解释两个基本扩展(Notification ContentNotification Service

Notification Content其实是用来自定义长按通知显示通知的自定义界面

Notification Service是用来处理远程通知的,我们可以在远程通知到来之际,我们在Notification Service

里面由30s的时间来处理这条通知的

首先使用Notification Service

 

1.创建扩展target

 

2. 这里会新建一个扩展的target,新的target需要新的ID,新的证书等(证书和id不会的请百度)

 

3.然后对主target进行设置

 

4. 对扩展的target进行设置

 

5.这里用的是极光推送,需要极光的相关文件(需要文件的可以去极光开发中心获取demo)

 

6.扩展target添加文件

 

7.当添加后为了可以访问http,在对应的plist文件进行设置

 

8.设置完成之后就可以看代码了

#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];
// self.bestAttemptContent.title = [NSString stringWithFormat:@"%@ [NotificationService]", self.bestAttemptContent.title];
NSString * attachmentPath = self.bestAttemptContent.userInfo[@"myAttachmentURL"];
//if exist 
if (attachmentPath) {
//download
NSURL *fileURL = [NSURL URLWithString:attachmentPath];
[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)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) {
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 {
//please invoke this func on release version
//[JPushNotificationExtensionService setLogOff];
//service extension sdk
//upload to calculate delivery rate
[JPushNotificationExtensionService jpushSetAppkey:@"3a6190XXXXXXX28907"];
[JPushNotificationExtensionService jpushReceiveNotificationRequest:request with:^ {
NSLog(@"apns upload success");
self.contentHandler(self.bestAttemptContent);
}];
}
- (void)serviceExtensionTimeWillExpire {
self.contentHandler(self.bestAttemptContent);
}
@end

其中的 "myAttachmentURL" 是自己定义的即可 (代码请仔细阅读,原理是获取网络图片,然后再显示)

 

9. 通过极光进行推送测试(目标平台选择开发环境即可,证书配置,请参考极光文档)

 

10.设置参数, 测试开始完成推送. "myAttachmentURL"是自己设置的哦!记得设置 mutable-content

 

11.收到推送

 

 12.Notification Service断点调试说明 

 

 选择target时并没有我们的扩展target,需要点击管理target进而点击Autocreate Schemes Now 

 

这个时候扩展的target出现了

 

好了可以运行了(会进行一次选择,选择我们的主app即可,运行后在 NotificationService.m 文件中的断点就可以捕捉到啦)

到这里Notification Service的使用说明就结束了,以后有补充的再写啦!

 

版权声明
本文为[甘林梦]所创,转载请带上原文链接,感谢
https://www.cnblogs.com/ljcgood66/p/10427877.html

编程之旅,人生之路,不止于编程,还有诗和远方。
阅代码原理,看框架知识,学企业实践;
赏诗词,读日记,踏人生之路,观世界之行;

支付宝红包,每日可领