IOS 10 Push must-see (Advanced 1)

Source: Internet
Author: User
Tags notification center

Source: Xu Different

Links: http://www.jianshu.com/p/3d602a60ca4f

IOS10 push must see (Basic article)

Although this article is relatively long, but also not good understanding, but still suggest that you collect, later use, you can see, have patience or read.

At the beginning of this article, I will tell you all about Apple's newly released iOS10 all notifications class.

First, the creation of local notification case detailed:

Attention Ah, small partners, local notifications must also be registered in the Appdelegate center, the switch is not open the notification does not matter, after all, is a local notice, but the notification of the receiving agent, as well as the notification of the click Agent, Apple has been merged. So we still need to write in the appdelegate of these 2 methods, and do not forget in-(BOOL) Application: (UIApplication *) application Didfinishlaunchingwithoptions: (nsdictionary *) launchoptions Registration Notification Center. If you use the Aurora push small partner, write a look at my basic article, hard everyone

Create an instance of the Unnotificationrequest class, be sure to set the identifier for it, in the back of the Find, update, delete notification, this identifier is used to distinguish this notification from other notifications
Add the request to Unusernotificationcenter and set the trigger to wait for the trigger
If another request has the same identity as the previous request, different content can be used to achieve the purpose of the update notification

Create a local notification we should first create a Unnotificationrequest class and add this class to Unusernotificationcenter. The code is as follows:

1. Create a Unnotificationrequest

NSString *requestidentifer = @"testrequest";

unnotificationrequest *Request = [unnotificationrequest requestwithidentifier: requestidentifer content:content trigger:trigger];

2. Add the Unnotificationrequest class to the current notification center

[[unusernotificationcenter currentnotificationcenter] addnotificationrequest:Request withcompletionhandler: ^(nserror * _nullable error) {

}];

When creating the Unnotificationrequest class, the official explanation is that a notification request can be notified to the user at the scheduled time and location. The triggering method is described in Unnotificationtrigger. Call this method when the notification is triggered. Replaces the notification request with the same identifier, and the number of messages is limited by the system.

The above translation, it may seem a bit awkward, simply put, we need to set an identifier for Unnotificationrequest, through the identifier, we can add to the notification, delete, update and other operations.

The following is the complete code to create the notification:

//1. Create Notification content

unmutablenotificationcontent *content = [[unmutablenotificationcontent alloc] init];

content. Title = @"Xu different test notice";

content. Subtitle = @"test Notification";

content. Body = @"from Xu different Jane book";

content. Badge = @1;

nserror *Error = nil;

NSString *Path = [[nsbundle mainbundle] pathforresource:@ "[email protected]" ofType:@"png"];

//2. Set Notification attachment contents

unnotificationattachment *att = [unnotificationattachment attachmentwithidentifier: @"att1" URL:[nsurl fileurlwithpath:path] options :nil error:&error];

if (error) {

NSLog(@"attachment error%@", error);

}

content. Attachments = @[att];

content. Launchimagename = @"[email protected]";

//2. Setting Sound

Unnotificationsound *Sound = [unnotificationsound defaultsound];

content. Sound = sound;

//3. Trigger Mode

Untimeintervalnotificationtrigger *Trigger = [untimeintervalnotificationtrigger Triggerwithtimeinterval:5 repeats:NO];

//4. Setting Unnotificationrequest

NSString *requestidentifer = @"testrequest";

unnotificationrequest *Request = [unnotificationrequest requestwithidentifier: Requestidentifer content:content trigger:trigger1];

//5. Add the notification to Unusernotificationcenter and the specified trigger point will be triggered

[[unusernotificationcenter currentnotificationcenter] addnotificationrequest: Request Withcompletionhandler: ^(nserror * _nullable error) {

}];

With the above code, we can create a 5 second trigger local notification, the specific style can be seen

drop-down Zoom content.launchimagename = @ "[email protected]"; the displayed picture is the effect of this line of code,

According to the above, you will find that when you create unnotificationrequest, you will need the two classes of unmutablenotificationcontent and Untimeintervalnotificationtrigger. I'll explain the relevant classes and class extensions below.

1.UNNotificationContent and unmutablenotificationcontent (notification content and variable notification content)

The notification content is divided into mutable and immutable types, similar to mutable arrays followed by immutable arrays. Later we update the notification with a specific identifier, which is a variable notification.

Whether it is a variable notification or an immutable notification, there are several properties:

1. Attachment array, Storage Unnotificationattachment class

@Property (ns_nonatomic_iosonly, copy) nsarray *Attachments ;

2. Application corner mark, 0 or not, meaning the corner mark disappears

@Property (ns_nonatomic_iosonly, copy, nullable) NSNumber *badge;

3. Main content

@Property (ns_nonatomic_iosonly, copy) NSString *body ;

4.app notification drop-down preview when showing the diagram

@Property (ns_nonatomic_iosonly, copy) NSString *launchimagename ;

5.UNNotificationSound class, you can set the default sound, or specify the name of the sound

@Property (ns_nonatomic_iosonly, copy, nullable) Unnotificationsound *sound ;

6. Sub-headings for push content

@Property (ns_nonatomic_iosonly, copy) nsstring *subtitle ;

7. Identification of the notification thread

@Property (ns_nonatomic_iosonly, copy) NSString * Threadidentifier;

8. The title of the push content

@Property (ns_nonatomic_iosonly, copy) NSString *title ;

9. Remote Notification push content

@Property (ns_nonatomic_iosonly, copy) nsdictionary *userInfo;

10.category identification

@Property (ns_nonatomic_iosonly, copy) NSString * Categoryidentifier;

The above properties, I have added the corresponding instructions, you can control my comments to use.

2.UNNotificationAttachment (Notification of attachment content)

In the Unnotificationcontent class, there is a property of an attachment array, which is the array containing the Unnotificationattachment class.

@Property (ns_nonatomic_iosonly, copy) nsarray *Attachments ;

Apple's explanation says that unnotificationattachment (attachment notification) is a notification that can contain audio, image or video content, and can display its contents. When you use local notifications, you can add attachments when the notification is created. For remote notifications, you must implement an extension that uses the Unnotificationserviceextension class notification service.

The way to create an attachment is AttachmentWithIdentifier:URL:options:error:. When used, you must specify the contents of the file attachment to be used, and the file format must be one of the supported types. After the attachment is created, it is assigned to the attachment property of the Content object. (For remote notifications, you must do this from your service extensions.) )

The type of attachment notification support is as follows:

Here's how to create a unnotificationattachment:

+ (nullable instancetype)attachmentwithidentifier:(nsstring *) Identifier URL:(nsurl *)URL options:(nullable nsdictionary * Options Error:(nserror *__nullable *__nullable)error;

Note: The URL must be a valid file path, or it will be an error

I'm going to go over here. Options Properties, a total of 4 choices (these properties can study dead me)

    • 1UNNotificationAttachmentOptionsTypeHintKey the value of this key is a nsstring that contains a description of the type uniform type identifier (UTI) of the file. If the key is not provided, the file name extension of the attachment determines its type, and the common type identifier is

      Kuttypeimage,kuttypejpeg2000,kuttypetiff,kuttypepict,kuttypegif, Kuttypepng,kuttypequicktimeimage and so on. See here you must have questions, these types of import error AH!! I researched the Apple document and found that you need to add the following framework to be able to do so, specifically, you can deal with the following types.

Attention:

The frame is #import.

Here's how to use it:

Dict[unnotificationattachmentoptionstypehintkey] = (__bridge ID _nullable) ( Kuttypeimage);

    • 2UNNotificationAttachmentOptionsThumbnailHiddenKey, is a bool value, for Yes, the thumbnail will be hidden and the default is yes.

As you can see from the chart above, it is clear where the map disappears.

Here's how to use it:

Dict[unnotificationattachmentoptionsthumbnailhiddenkey] = @YES;

    • 3UNNotificationAttachmentOptionsThumbnailClippingRectKey thumbnail of the clipping rectangle. The value of this key is a normalized cgrect– that is, a rectangle of a cell whose value is at 1.0? 0.0, which represents the part of the original image to be displayed in the dictionary. For example, the origin and size (0.5, 0.5) of the specified (0.25, 0.25) defines the clipping rectangle, showing only the center portion of the image. Use the Cgrectcreatedictionaryrepresentation function to create a rectangle for the dictionary.

The above sentence is an Apple translation, too round the mouth. I say simply, this is the picture below me.

The whole picture is divided, the overall ratio is 1, if you want to plot the shadow area, you need to write the CGRect (0.5,0.5,0.25,0.25), meaning, from (0.5,0.5) as the origin, the area is (0.25,0.25), we can understand, that is, the following method.

Here's how to use it:

Dict[unnotificationattachmentoptionsthumbnailclippingrectkey] = (__bridge ID _nullable ( cgrectcreatedictionaryrepresentation (cgrectmake(0.5, 0.5, 0.25 ,0.25 )));

Using the above method, you can get an image of the shaded part of the graph, which will be the thumbnail of the notification. For example, I have the following picture, thumbnails you should be able to find the change it.

Here, in order to understand, we are saying a few "coordinate points":

(0,0,0.25,0.25) The smallest square in the upper-left corner

(0,0,0.5,0.5) One-fourth Square, upper left corner

(0.5,0.5,0.5,0.5) One-fourth Square, lower right corner

(0.5,0,0.5,0.5) One-fourth Square, lower left corner

(0.25,0.25,0.5,0.5) The most central square

Special attention:

debugging here, I feel that the apple should have a bug, that is, I am changing the frame to show the thumbnail, back and forth, always displayed as the first time to write the frame. After I modify the Requestidentifer property of Unnotificationrequest, I can transform the property. So I guess the same requestidentifer notice, calculate a notification, so only call the updated method, to change the thumbnail to eat not greasy bar, perhaps not a bug.

    • 4UNNotificationAttachmentOptionsThumbnailTimeKey, the general video attachment will be used, refers to the film in a second to do this thumbnail image;

Here's how to use it:

dict[unnotificationattachmentoptionsthumbnailtimekey] =@ten;

Here we can directly pass a NSNumber value, for example, use the picture of the 10s film to do the thumbnail image according to the above to write. Also, be aware that this number of seconds must be within the length range of the movie, or error.

3.UNTimeIntervalNotificationTrigger (notification trigger mode)

This is me! (This article has already been introduced, and now I'm going to cover it in detail.) [Www.baidu.com] This article has been introduced in the preliminary, and now I am in the detailed introduction.

    • 1.UNPushNotificationTrigger (remote notification trigger) We don't normally use it.

    • 2.UNTimeIntervalNotificationTrigger (local notification) after a certain amount of time, push notifications are repeated or not repeated. We can set TimeInterval (interval) and repeats (repeat).

      How to use:

Untimeintervalnotificationtrigger *Triggerone = [untimeintervalnotificationtrigger Triggerwithtimeinterval:5 repeats:NO];

Explanation: The above method means that it is executed after 5 seconds. Repeats this property, if necessary for repeated execution, then timeinterval must be greater than 60s, otherwise will be reported * terminating app due to uncaught exception ' Nsinternalinconsistencyexception ', Reason: ' time interval must is at least if repeating ' error!**

    • 3.UNCalendarNotificationTrigger (local notification) after a certain date, push notifications are repeated or not repeated for example, you push a notification 8 points a day, requiring only a datecomponents of 8. If you want to push this notice at 8 every day, just repeats is yes.

Monday 8:00 to work

Nsdatecomponents *Components = [[nsdatecomponents alloc] init];

Note that weekday is starting from Sunday, if you want to set it to start from Monday, you can think about it.

Components. Weekday = 2;

Components. Hour = 8;

Uncalendarnotificationtrigger *Trigger = [uncalendarnotificationtrigger Triggerwithdatematchingcomponents:components repeats:YES];

    • 4.UNLocationNotificationTrigger (local notification) a notification of geolocation, using this notification, you need to import

      #import这个系统类库. The sample code is as follows:

1, if the user enters or out of an area will call the following two methods

- (void)locationmanager:(Cllocationmanager *)manager

didenterregion:(clregion *) Region

- (void)locationmanager:(Cllocationmanager *)Manager

didexitregion:(clregion *) RegionAgent method Feedback related information

2, one to a latitude and longitude of the notice, the judge contains a certain point

Use of!!!!!! is not recommended Clregion *region = [[Clregion alloc] init];//does not recommend the use of!!!!!!

Clcircularregion *Circlarregin = [[clcircularregion alloc] init];

[circlarregin containscoordinate:(cllocationcoordinate2d)];

Unlocationnotificationtrigger *trigger4 = [unlocationnotificationtrigger Triggerwithregion:circlarregin repeats:NO];

Note that it is recommended to use Clcircularregion, a class that inherits from the Clregion, because I see that Apple has flown up the clregion that contains this, and it is recommended that we use the Clcircularregion type

IOS 10 Push must-see (Advanced 1)

Related Article

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.