IOS10 local message push download images

It was the girlfriend’s orders to come down


It is now more fire scene intelligent. Real time location of the project will monitor the user, such as when the user to KFC, APP will push a push message with the picture, the picture can be introduced AD / / coupons and the like.

requirement analysis

1.iOS10 can be added in the push picture
2 push to use the local push can be
back to the background of the picture is a section of URL, need to download and save their own local


1 registration notice

At the beginning of the iOS10, Apple will be notified to unify the UserNotifications class, the first to import this class

#import < UserNotifications/UserNotifications.h>

Application authority

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

2 initialization notification

//2. *content = [[UNMutableNotificationContent alloc] initialization notification UNMutableNotificationContent init]; content.badge = @1; content.title = @ "announcement"; content.body = @ "my wife the most beautiful gentle ~ ~"; / / if you do not fill in the body, will not be UNNotificationSound *sound = [UNNotificationSound defaultSound] pop-up notification system; / / voice content.sound = sound;

3 download the picture and save it in the sandbox

Save the push button in the sandbox will be automatically deleted after the completion of the push

//3. download //3.1 image NSString * imageUrlString = @ ""; NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:imageUrlString]]; //3.2 NSString * documentPath save the picture to the sandbox (NSDocumentDirectory = NSSearchPathForDirectoriesInDomains, NSUserDomainMask, YES).FirstObject; NSString *localPath [documentPath stringByAppendingPathComponent:@ = "localNotificationImage.jpg"]; [imageData writeToFile:localPath atomically:YES]; //3.3 attachment set the notification (Annex) if (localPath & & isEqualToString:@ "[localPath!") {UNNotificationAttachment = [UNNotificationAttachment * attachment attachmentWithIdentifier:@ photo "URL:[NSURL URLWithString:[@" stringByAppendingString:localPath]] "options:nil error:nil]; if (attachment) {content.attachments = @[attachment];}}

Where URL must be a file path, so the need to splice 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 set trigger points and push

//4. set UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:2 trigger mode repeats:NO]; //5. *request = [UNNotificationRequest requestWithIdentifier:@ push UNNotificationRequest "pusswzy" content: content trigger:trigger]; UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; [center addNotificationRequest:request withCompletionHandler:^ (NSError * _Nullable error) {if (error) {NSLog ("% @ @", error);}}]; / / set the proxy center.delegate = self;

5 monitor UNUserNotificationCenterDelegate

This method is called when a message is received at the front desk

#pragma mark - UNUserNotificationCenterDelegate - (void) userNotificationCenter: (UNUserNotificationCenter * Center) willPresentNotification: (UNNotification * notification) (withCompletionHandler: (void ^) (UNNotificationPresentationOptions)) {/ / completionHandler completionHandler display (UNNotificationPresentationOptionAlert|UNNotificationPresentationOptionSound); / / show / / completionHandler (UNNotificationPresentationOptionNone);}

Achievement display

IOS10 local message push download images
initial page

Reception received notice

IOS10 local message push download images
front desk

Background received notification

IOS10 local message push download images
IOS10 local message push download images

It’s really cool for a man to write some code for his girlfriend