Recommended reading:
Basic knowledge of IOS10 push (must see article)
Starting with this article, I'll talk to you all about Apple's new IOS10 announcement class.
First, create a local notification case detailed:
Attention Ah, small partners, local notice must also be in the Register center in the appdelegate, notice the switch does not open, after all, is local notice, but notice of the agent, and notify the click of the agent, Apple to one. So you still need to write these 2 methods in the Appdelegate, and don't forget to application in-(BOOL): (uiapplication *) application Didfinishlaunchingwithoptions: (nsdictionary *) launchoptions Registration Notification Center. If you use the Aurora push small partner, write a look at my basic article, hard everybody
To create an instance of the Unnotificationrequest class, be sure to set the identifier for it, in the back of the lookup, update, delete notification, this logo is can be used to distinguish between this notice and other notifications
Add the request to the 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 update the notification.
To 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 notify the user at the time and place of the reservation. The triggering method is shown in the Unnotificationtrigger. Call this method when the notification is triggered. will replace the notification request with the same identifier, in addition, the number of messages is limited by the system.
The translation above may seem a bit clumsy, in short, we need to set an identifier for the unnotificationrequest, through the identifier, we can add to the notification, delete, update and other operations.
Here is the complete code to create the alert:
1. Create notification content Unmutablenotificationcontent *content = [[Unmutablenotificationcontent alloc] init];
Content.title = @ "Xu different test notice";
Content.subtitle = @ "Test notice";
Content.body = @ "from Xu different Jane book";
Content.badge = @1;
Nserror *error = nil;
NSString *path = [[NSBundle mainbundle] pathforresource:@ "icon_certification_status1@2x" ofType:@ "PNG"]; 2. Set the content of the notification attachment unnotificationattachment *att = [unnotificationattachment attachmentwithidentifier:@ "ATT1" URL:[NSURL
Fileurlwithpath:path] Options:nil error:&error];
if (Error) {NSLog (@ "Attachment error%@", error);} content.attachments = @[att];
Content.launchimagename = @ "icon_certification_status1@2x";
2. Set sound Unnotificationsound *sound = [Unnotificationsound defaultsound];
Content.sound = sound; 3. Trigger mode Untimeintervalnotificationtrigger *trigger = [Untimeintervalnotificationtrigger triggerwithtimeinterval:5
Repeats:no];
4. Set Unnotificationrequest nsstring *requestidentifer = @ "Testrequest"; Unnotificationrequest *request = [Unnotificationrequest reQuestwithidentifier:requestidentifer content:content Trigger:trigger1]; 5. Add the notice to the Unusernotificationcenter, to the specified trigger point will be triggered [[Unusernotificationcenter Currentnotificationcenter] Addnotificationrequest:request withcompletionhandler:^ (Nserror * _nullable error) {}];
Through the above code, we can create a 5-second trigger local notification, the specific style can look at the following figure
drop-Zoom Content.launchimagename = @ "icon_certification_status1@2x"; the picture shown is the effect of this line of code, as shown in
According to the above, you will find that when you create a unnotificationrequest, you will need unmutablenotificationcontent and Untimeintervalnotificationtrigger these two classes. Below I will explain the related classes, as well as the class extensions.
1.UNNotificationContent and unmutablenotificationcontent (notification content and variable notification content)
The notification content is divided into mutable and immutable types, similar to variable arrays and mutable arrays. Later we update the notification with a specific identifier, or we use a variable notification.
Whether it is a variable or immutable notification, there are several properties:
1. Attachment array, Storage Unnotificationattachment class
@property (ns_nonatomic_iosonly, copy) Nsarray *attachments;
2. The application corner mark, 0 or not, means that the corner mark disappears
@property (ns_nonatomic_iosonly, copy, nullable) NSNumber *badge;
3. Main content
@property (ns_nonatomic_iosonly, copy) NSString *body;
4.app Notice Drop-down preview Show
@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. Push the contents of the sub-title
@property (ns_nonatomic_iosonly, copy) NSString *subtitle;
7. Notification thread identification
@property (ns_nonatomic_iosonly, copy) NSString *threadidentifier;
8. Push the contents of the title
@property (ns_nonatomic_iosonly, copy) NSString *title;
9. Remote Notification push content
@property (ns_nonatomic_iosonly, copy) nsdictionary *userinfo;
10.category logo
@property (ns_nonatomic_iosonly, copy) NSString *categoryidentifier;
The above attributes, I have added a corresponding description, you can compare my comments to use.
2.UNNotificationAttachment (Attachment content Notice)
In the Unnotificationcontent class, there is an array of attachment properties, which is the array containing the Unnotificationattachment class.
@property (ns_nonatomic_iosonly, copy) Nsarray *attachments;
Apple explains that unnotificationattachment (attachment notification) is a notification that can contain audio, image, or video content and can display its contents. When you use local alerts, you can add attachments when the notification is created. For remote notifications, you must implement notification service extensions using the Unnotificationserviceextension class.
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. When an attachment is created, it is assigned to the attachment property of the Content object. (For remote notifications, you must do this from your service extension.) )
The type of attachment notification supports the following figure:
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 say this again. The properties of options, a total of 4 options (these properties can be studied dead me)
1UNNotificationAttachmentOptionsTypeHintKey the value of this key is a nsstring of a type uniform type identifier (UTI) that contains a description file. If you do not provide the key, the file name extension of the attachment determines its type, and the commonly used type identifier has
Kuttypeimage,kuttypejpeg2000,kuttypetiff,kuttypepict,kuttypegif, Kuttypepng,kuttypequicktimeimage and so on. See here you must have questions, these types of import error!! I studied the Apple documentation, and found that you need to add the following framework, you can use the following types to deal with.
Attention:
The frame is #import.
Use the following methods:
Dict[unnotificationattachmentoptionstypehintkey] = (__bridge ID _nullable) (kuttypeimage);
2UNNotificationAttachmentOptionsThumbnailHiddenKey, is a bool value, when yes, thumbnails will be hidden, the default is yes. As shown in figure:
As you can see from the above picture, you know where the picture disappears.
Use the following methods:
Dict[unnotificationattachmentoptionsthumbnailhiddenkey] = @YES;
A thumbnail of the 3UNNotificationAttachmentOptionsThumbnailClippingRectKey clipping rectangle. The value of this key is a normalized cgrect– that is, a cell rectangle whose value is a dictionary of the parts of the original image to be displayed in 1.0〜0.0. For example, the origin and size (0.5, 0.5) of the specified (0.25, 0.25) define 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 around the mouth. I'm simply saying, this is my picture below.
The whole picture is divided, the overall ratio of 1, if you want the shadow area, you need to write 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.
Use the following methods:
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 shadow portion of a picture, which is a thumbnail of the notification. For example, I have the following figure, thumbnails you should be able to see the change.
Here to understand, in giving you a few "coordinates point":
(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 center of the square
Special attention:
When I was debugging here, I felt that there was a bug in the apple that I changed back and forth as I was changing the frame to show the thumbnail, always showing the frame for the first time. After I modify the Unnotificationrequest Requestidentifer property, I can transform the property. So I guess the same requestidentifer notice, calculate a notice, so can only call the updated method, to change the thumbnail is not greasy eat, perhaps not a bug.
4UNNotificationAttachmentOptionsThumbnailTimeKey, the general film accessories will be used, refers to the film with a second to do this thumbnail;
Use the following methods:
Dict[unnotificationattachmentoptionsthumbnailtimekey] =@10;
Here we can directly pass a NSNumber value, such as using the 10s image of the film to do the thumbnail, according to the above to write. In addition, note that this number of seconds must be within the length of the film, or else the error.
3.UNTimeIntervalNotificationTrigger (notification trigger mode)
This is me! (This article is a preliminary introduction, now I am in detail) [Www.baidu.com] This article has been tentatively introduced, and now I'm going to introduce it in detail.
1.UNPushNotificationTrigger (remote notification trigger) We don't normally use it.
2.UNTimeIntervalNotificationTrigger (local notification) after a certain time, repeat or do not repeat the push notification. We can set the TimeInterval (time interval) and repeats (whether to repeat).
How to use:
Untimeintervalnotificationtrigger *triggerone = [Untimeintervalnotificationtrigger triggerWithTimeInterval:5 Repeats:no];
Explanation: The above method refers to execution after 5 seconds. Repeats this property, if required for repeated execution, then timeinterval must be greater than 60s, otherwise it will be reported * terminating app due to uncaught exception ' Nsinternalinconsistencyexception ', Reason: ' time interval must the error least ' at repeating
3.UNCalendarNotificationTrigger (local notification) after a certain date, repeat or do not repeat the push notification for example, you push a notice 8 points a day, only need datecomponents for 8. If you want to push this notice every 8 O ' Day, as long as repeats is yes.
Monday 8:00 work
nsdatecomponents *components = [[Nsdatecomponents alloc] init];
Note that the weekday is starting from Sunday, if you want to set to start from Monday, everyone can think about it ~
Components.weekday = 2;
Components.hour = 8;
Uncalendarnotificationtrigger *trigger = [Uncalendarnotificationtrigger triggerwithdatematchingcomponents: Components Repeats:yes];
4.UNLocationNotificationTrigger (local notification) a notice of geographical location, using this notice, you need to import
#import这个系统类库. The sample code is as follows:
//1, if the user enters or walks out of an area, the following two methods are called
-(void) Locationmanager: (Cllocationmanager *) Manager
Didenterregion: (clregion *) region
-(void) Locationmanager: (Cllocationmanager *) Manager
Didexitregion: ( Clregion *) Region Agent Method Feedback Information
//2, a certain latitude and longitude on the notice, Judge contains a certain point
//Do not recommend the use of!!!!!! 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 that you use Clcircularregion, which inherits from Clregion, because I have seen that Apple has flown the clregion contains this point and recommended that we use the Clcircularregion type
The above is a small set to introduce the iOS 10 push high order article (must see), I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!