IOS10 Push Tutorial Detailed _ios

Source: Internet
Author: User

Last month received a demand to do a IOS10 push, intent on hitting the app store headlines. Instant crazy, tools are not, so hurriedly install Xcodebeta version, Ios10beta version, and then began endless search information, after all, new features, after all did not do ... But fortunately, before the press conference out, because I am lazy, dragged to write now, the next is to witness the miracle of the moment!

Principle

In the picture, provider refers to a push server for an iphone software that I will use. NET as a provider.

APNs is the apple push Notification Service (Apple push server) abbreviation, is the MAC server.

The above diagram can be divided into three stages.

The first stage:. NET application to send the message, the purpose of the iphone logo packaging, sent to APNs.

Second Stage: APNs the iphone in its own list of registered push services, looking for a corresponding logo and sending the message to the iphone.

Phase III:The iphone sends messages to the appropriate application and pops the push notification as set.

As we can see from the above picture.

1, the first is the application registration message push.

2, iOS and APNs server to Devicetoken. The application accepts Devicetoken.

3. The application sends Devicetoken to the push server program.

4. The service-side program sends messages to the APNS service.

5, the APNs service sends the message to the iphone application.

After XCODE8 test environment certificate can be automatically generated, so no more said.

Create

The Today extension, which wrote iOS9 long ago, is similar to the need to create a target,

As shown in the figure, Notification content is responsible for customizing the notification ui,notification Service extension is responsible for receiving and processing data

is living
iOS expansion class is not independent network request data, but before doing today to check some other apps, Drop-down notification bar when some of the app can catch the package, it is estimated to do a single network request encapsulation, just guess, if there is a similarity, It's a coincidence. But the notice doesn't have to be that much trouble. First pick a picture from the Internet (qualify HTTPS protocol): https://homeba.s3.amazonaws.com/__sized__/scene/ 2c0f3bdb7715fed7190fd87e5e5340e4-1473387950-crop-c0-5__0-5-590x442-85.jpg, push format and can be optional, details visible: https:// developer.apple.com/library/ios/documentation/networkinginternet/conceptual/remotenotificationspg/chapters/ Thenotificationpayload.html.

The image above is the format I used, the contents of "APS" inside iOS will automatically get the collation.

Alert is the textual content of the notification,

Sound is the notification sound, which takes the default,

Badge Whether the program badge is displayed,

The point is mutable-content, this field determines the call custom notification interface, which is the notification content controller

Category is a value that is negotiated with the background, shows the action, that is to tell the button below, can extend some operations without entering the program.

"APS" can be added to the outside of the required fields, which depends on the specific requirements.

The preparation for the push tool is complete, and the following starts with dry code.

-(void) Didreceivenotificationrequest: (unnotificationrequest *) Request Withcontenthandler: (void (^) ( Unnotificationcontent * _nonnull)) ContentHandler {self.contenthandler = ContentHandler; self.bestAttemptContent = [

Request.content Mutablecopy];
NSString * Attchurl = [Request.content.userInfo objectforkey:@ "image"];

Download the picture and place it in the local uiimage * Imagefromurl = [self getimagefromurl:attchurl];
Obtain documents directory Nsarray * paths = Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES);

NSString * documentsDirectoryPath = [paths objectatindex:0]; NSString * LocalPath = [self saveimage:imagefromurl withfilename:@ ' myimage ' oftype:@ ' png ' indirectory:
documentsDirectoryPath]; if (LocalPath &&![ LocalPath isequaltostring:@ ""]) {unnotificationattachment * attachment = [unnotificationattachment attachmentwithidentifier:@ "Photo" Url:[nsurl urlwithstring:[@ "file://" Stringbyappendingstring:localpath]] Options
 : Nil Error:nil]; if (attachment) {self.bestAttemptContent.atTachments = @[attachment];
} self.contenthandler (Self.bestattemptcontent);

 }

Because it is not convenient to use the Sdimage framework, so the network request can only let go, have plenty of money, in addition, IOS10 notice although you can load pictures, but can only load local pictures, so here need to do a processing, first the picture request, then save to the local

-(UIImage *) Getimagefromurl: (NSString *) FileURL {NSLog (@ "Execute picture download function");
UIImage * result;
Datawithcontentsofurl method requires HTTPS connection NSData * data = [NSData Datawithcontentsofurl:[nsurl Urlwithstring:fileurl]];

result = [UIImage imagewithdata:data];
return result; //Save downloaded pictures to local-(NSString *) SaveImage: (UIImage *) image withfilename: (NSString *) imagename ofType: (NSString *) Extension indirectory: (NSString *) DirectoryPath {nsstring *urlstr = @ ""; if ([[Extension lowercasestring] Isequaltostrin g:@ "PNG"]) {urlstr = [directorypath stringbyappendingpathcomponent:[nsstring stringwithformat:@ "%@.%@", ImageName, @ "
 PNG "]];
[Uiimagepngrepresentation (image) Writetofile:urlstr options:nsatomicwrite Error:nil];
  else if ([[Extension lowercasestring] isequaltostring:@ "jpg"] | |
[[[Extension lowercasestring] isequaltostring:@ ' jpeg '])
 {urlstr = [directorypath stringbyappendingpathcomponent:[nsstring stringwithformat:@ "%@.%@", ImageName, @ "jpg"]]; [UIImageJPEGRepresentation (Image, 1.0) writetofile:uRlstr Options:nsatomicwrite Error:nil];
else {NSLog (@ "extension error");} return urlstr;

 }

Then the data is easy to complete, and the final step is to customize the UI and get the picture. The storyboard is still very useful.

A good constraint can add pictures!

-(void) Didreceivenotification: (unnotification *) notification {
NSLog (@ "hehe");
Self.label.text = Notification.request.content.body;
Unnotificationcontent * content = notification.request.content;
Unnotificationattachment * attachment = Content.attachments.firstObject;
if (attachment. Url.startaccessingsecurityscopedresource) {
 self.imageView.image = [UIImage imagewithcontentsoffile:attachment . Url.path];
}


It's that simple, it's done!!! (a big sigh of relief ...)

To an effect picture, haha

This article has been organized into the "iOS push Tutorial", welcome to learn to read.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.