序言
今天闲着,我就把之前项目中做过的一些技术点写出来分享下,希望能帮到人。第一次写,写的不好的话请多多包涵哈!!!
背景
带上图片的推送使用范围很广,既可以是安防产品的报警推送时附带图片,又可以是广告时的宣传等等。
1.iOS 10以后才可以在推送中附加图片
2.推送可以是本地推送和后台推送
3.后台推送返回的图片是一段URL,当本地下载到临时目录使用后会自动删除,当然你也可以自己本地下载并保存
步骤
1.导入头文件并遵守协议
#import <UserNotifications/UserNotifications.h>
@interface AppDelegate ()<UNUserNotificationCenterDelegate>
2.实现
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
//如果你想自己手动推送,可以在ViewController里加个按钮发送以下通知事件
// [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pushAction) name:@"pushWithPhoto" object:nil];
[self pushAction];
return YES;
}
-(void)pushAction
{
//1.申请权限
[[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert completionHandler:^(BOOL granted, NSError * _Nullable error){}];
//2.初始化通知
UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
content.badge = @1;
content.title = @"通知的标题";
content.body = @"通知的内容"; //如果不填写body,则不会弹出通知
UNNotificationSound *sound = [UNNotificationSound defaultSound]; //系统的声音
content.sound = sound;
//3.下载图片
//3.1获取图片
NSString * imageUrlString = @"https://img1.doubanio.com/img/musician/large/22817.jpg";
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:imageUrlString]];
//3.2图片保存到沙盒
NSString *documentPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;
NSString *localPath = [documentPath stringByAppendingPathComponent:@"localNotificationImage.jpg"];
[imageData writeToFile:localPath atomically:YES];
//3.3设置通知的attachment(附件)
if (localPath && ![localPath isEqualToString:@""]) {
UNNotificationAttachment * attachment = [UNNotificationAttachment attachmentWithIdentifier:@"photo" URL:[NSURL URLWithString:[@"file://" stringByAppendingString:localPath]] options:nil error:nil];
if (attachment) {
content.attachments = @[attachment];
}
}
//4.设置触发模式
UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:2 repeats:NO];
//5.推送
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"pusswzy" content:content trigger:trigger];
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
if (error) {
NSLog(@"%@", error);
}
}];
//设置代理
center.delegate = self;
}
#pragma mark - iOS10 收到通知(本地和远端) UNUserNotificationCenterDelegate
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler{
// 有这句,推送会在前台展示
completionHandler(UNNotificationPresentationOptionAlert|UNNotificationPresentationOptionSound);
}
以上是简单的本地推送图片的实现步骤,你直接复制代码就可以使用。下一篇将会介绍远程推送图片方面的。
参考资料:
iOS10本地消息推送下载的图片