iOS10本地消息推送下载的图片

又是女朋友大人吩咐下来的任务~
源码链接

场景

就是现在比较火的场景智能化.项目会监听用户的实时位置,例如当用户到了肯德基,APP会推送一条带有图片的推送消息,图片可以是广告/介绍/优惠券之类的.

需求分析

1.iOS10才可以在推送中附加图片
2.推送使用本地推送就可以
3.后台返回的图片是一段url,需要自己本地下载并保存

实现

1.注册通知

在iOS10开始,苹果将通知统一到了UserNotifications这个类中,首先导入这个类

#import 

申请权限

//1.
[[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert completionHandler:^(BOOL granted, NSError * _Nullable error) {
        NSLog(@"%d--%@", granted, error);
    }];;

2.初始化通知

//2.初始化通知
    UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
    content.badge = @1;
    content.title = @"通知的标题";
    content.body = @"我老婆最温柔~最美丽~";   //如果不填写body,则不会弹出通知
    UNNotificationSound *sound = [UNNotificationSound defaultSound]; //系统的声音
    content.sound = sound;

3.下载图片并保存在沙盒

保存在沙盒的推送图片 会在推送完成后自动删除

//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];
        }
    }

其中url必须是文件路径, 所以需要拼接file://

// Creates an attachment for the data at URL with an optional options dictionary. URL must be a file URL. Returns nil if the data at URL is not supported.
+ (nullable instancetype)attachmentWithIdentifier:(NSString *)identifier URL:(NSURL *)URL options:(nullable NSDictionary *)options error:(NSError *__nullable *__nullable)error;

4.设置触发点并推送

   //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;

5.监听UNUserNotificationCenterDelegate

当在前台收到消息的时候会调用这个方法

#pragma mark - UNUserNotificationCenterDelegate
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler
{
       // 展示
 completionHandler(UNNotificationPresentationOptionAlert|UNNotificationPresentationOptionSound);
    
      // 不展示
      //    completionHandler(UNNotificationPresentationOptionNone);
}

成果展示

初始页面

前台收到通知

前台

后台收到通知

Jay

一个男人能帮自己的女朋友 写点代码的感觉真爽

你可能感兴趣的:(iOS10本地消息推送下载的图片)