Source: Xu Different (@2016 Xu Xiao)
Links: http://www.jianshu.com/p/45933f5450a4
Everybody wait ~ This is definitely the most complete the most detailed unnotificationcontentextension explanation yo ~
In this article, I will give you to fill in the next part of the push content, I hope you read this article, you can easily play retweets sent.
1, Unnotificationcontentextension
1.1, Unnotificationcontentextension Introduction
In short, unnotificationcontentextension This class, is also a new feature of IOS10 push, official documents in such a sentence, a simple explanation, presents a custom interface for a delivered local or remote notification. (When you receive a remote or local notification, a custom interface pops up). The effect is as shown
The two graphs above are the effects we are going to make, in the area of custom view, you can put a video, put a calendar, put a label that shows the location, in a word, we can customize view!
1.2, how to create a new unnotificationcontentextension
Create the target for this unnotificationcontentextension, similar to the steps for creating unnotificationserviceextension.
First step: Create
Step Two: Select Create Type
You can then see that the content was generated
1.3. How to use and related demo
Speaking of how to use this class, we must first say, the system helps us to generate the several files represent what meaning.
>0.info.plist
Under this nsextensionattributes dictionary, we have three properties to add
I. Unnotificationextensioncategory.
(Must have, system has been created well)
Explanation: The value corresponding to this key can be a string, or an array, each string is a identifier, This identifier corresponds to the attributes of each unmutablenotificationcontent categoryidentifier.
To put it simply, we can let the server bring this notice to the categoryidentifier when we receive the notice, the function is that we can customize our notification content according to the video, music, picture, respectively. Different classification identifiers will also help us to distinguish what types of notifications we have when we talk about unnotificationaction, so that we can make different actions for different types of notifications. Above, I was in the form of a string. In the form of an array:
When using, we refer to the following code:
1. This method is the Unnotificationserviceextension class inside the method, can refer to my previous article on the unnotificationserviceextension, I want to rewrite my notice here.
- (void)didreceivenotificationrequest:(unnotificationrequest *)request Withcontenthandler:(void (^) (unnotificationcontent * _nonnull ))ContentHandler {
Self. ContentHandler = ContentHandler;
//copy sent notification, start to do some processing
self. Bestattemptcontent = [request. Content mutablecopy];
//Modify the notification content here ...
self. Bestattemptcontent. Title = [nsstring stringwithformat:@"%@ [modified]", self . Bestattemptcontent. Title];
//rewrite something
self. Bestattemptcontent. Title = @"I am the title";
self. Bestattemptcontent. Subtitle = @"I am a sub-title";
self. Bestattemptcontent. Body = @"from Xu Different";
//Accessories
nsdictionary *dict = self. Bestattemptcontent. UserInfo;
nsdictionary *notidict = dict[@"APS"];
NSString *imgurl = [nsstring stringwithformat:@"%@",notidict[@ "imageabsolutestring"];
!!!!! Here's the point!!!!!!!!!!!!.
//I am here to write dead category1, in fact, when receiving the system push, each push content preferably with a catagory, with the server agreed, so that we easily according to Categoryidentifier to customize different types of views, and Action
self. Bestattemptcontent. Categoryidentifier = @"Category1";
}
Attention, everybody.
!!!!! Here's the point!!!!!!!!!!!!.
//I am here to write dead category1, in fact, when receiving the system push, each push content preferably with a catagory, with the server agreed, so that we easily according to Categoryidentifier to customize different types of views, and Action
self. Bestattemptcontent. Categoryidentifier = @"Category1";
Set Categoryidentifier here, it is best to let the server push content with this, and then we better customize. It is not recommended to write dead locally.
For details, refer to my demo address
Two. Unnotificationextensioninitialcontentsizeratio.
(Must have, system has been created well)
Explanation: The type of this value is a floating-point type and represents the ratio of height to width. The system uses this ratio as the size of the initialized view. For a simple example, if the value is 1, the view is square. If 0.5, the height is half the width.
Note that this value is just the initialization of a value, after the extension is added, you can rewrite the frame, when the display, when we have not opened this view preview, the background is a similar image placeholder gray, the height of the gray width ratio, is set by this value.
Three. Unnotificationextensiondefaultcontenthidden.
(optional)
Explanation: This value is a bool value, and when yes, hides the previously pushed view of the content, showing only our custom views. (because in custom view, we can get the push content, and then according to the layout we want to show) if no (default is NO), the push view will have both our custom view, there will be the system's original push content view (the attachment is not displayed here, Only the text inside the body will be displayed)
Four. As for Nsextensionmainstoryboard and Nsextensionpointidentifier, the system is generated by default, you can use it directly, if you need to change, only change the name of the storyboard used (but no one should have deleted the system. O (∩_∩) o)
>1.maininterface.storyboard
This is a simple storyboard file with a view inside it, which is the custom view view in the layer above. It is bound to Notificationviewcontroller.
>2.notificationviewcontroller
This is the system to help us by default to create a controller, inherited Uiviewcontroller, is actually a controller.
Speaking of this controller, we need to explain a method:
1. Comply with the Unnotificationcontentextension agreement, we need to use the method
This method means that you will call this method as soon as you receive the notification and ensure that the Categoryidentifier settings are the same as those set in Info.plist. Note: You can call this method when you receive multiple notifications for a session, and each notification is received.
- (void)didreceivenotification:(unnotification *)notification;
Use the following:
- (void)didreceivenotification:(unnotification *)notification {
This method, you can give their own control assignment Ah, adjust frame ah, and so on, I print out here the content of the notice for everyone to use.
nsdictionary *dict = notification. Request. Content. UserInfo;
//Here you can take all the printed things out
NSLog(@"%@",dict);
/**************************** Printed information is ************
APS = {
Alert = "This is some fancy message.";
badge = 1;
from = "Hello everyone, I am xu different";
imageabsolutestring = "Http://upload.univs.cn/2012/0104/1325645511371.jpg";
"Mutable-content" = 1;
Sound = default;
};
}
*******************************************/
}
Speaking of which, the simple unnotificationcontentextension has already been said, I am to add a little to you. In UNNotificationContentExtension.h, there is such an enumeration
typedef Ns_enum(nsuinteger, unnotificationcontentextensionmediaplaypausebuttontype ) {
//No Play button
unnotificationcontentextensionmediaplaypausebuttontypenone,
//Have play button, after clicking Play, the button still exists, like the switch of music playing
unnotificationcontentextensionmediaplaypausebuttontypedefault,
//There is a Play button, after clicking the play button disappears, click again to pause the play button to restore
unnotificationcontentextensionmediaplaypausebuttontypeoverlay,
}
See this enumeration, we must be wondering how to use AH. Take a look at some of the following properties
Set the properties of the play button
@Property (nonatomic, readonly, assign) Unnotificationcontentextensionmediaplaypausebuttontype mediaplaypausebuttontype;
Set the play button's frame
@Property (nonatomic, readonly, assign) CGRect mediaplaypausebuttonframe;
Set the color of the play button
@Property (nonatomic, readonly, copy) Uicolor * Mediaplaypausebuttontintcolor;
Start and pause playback
- (void)mediaplay;
- (void)mediapause;
There are also the following classes, although this class also has a way to start playing with the end of playback, but note that this belongs to Nsextensioncontext, and the above is the way we talk about the Unnotificationcontentextension protocol method. Everyone should pay attention.
@Interface nsextensioncontext (unnotificationcontentextension)
Control playback
- (void)mediaplayingstarted
Control Pause
- (void)mediaplayingpaused
@End
To see these properties and want to know how to use them, see my steps below:
Analysis:
First of all, these properties are readonly, so the direct use of self. property to modify is definitely an error, so we can only use the Get method.
Second: Depending on the type of button, we can think that if the button is not available, this playback starts to pause the method also useless. If there is a button, naturally we have a play operation, Lenovo other UI space, we have to be sure to rewrite its frame to determine his position. Set the color to set its display color. Set the button's type so that he shows it.
Returns the button for the default style
- (unnotificationcontentextensionmediaplaypausebuttontype)mediaplaypausebuttontype
{
return unnotificationcontentextensionmediaplaypausebuttontypedefault;
}
Returns the frame of the button
- (cgrect)mediaplaypausebuttonframe
{
return cgrectmake( n , +, +);
}
Returns the color of the button
- (uicolor *)mediaplaypausebuttontintcolor{
return [uicolor bluecolor];
}
With the code above, our button can be displayed. such as (please ignore the following policies and other buttons):
Location, you can override the frame to determine the position of the button.
When we click on the blue button, we can perform some playback pauses, as follows
- (void)mediaplay{
NSLog(@"Mediaplay, start playing");
}
- (void)mediapause{
NSLog(@"mediapause, pause Playback");
}
There's one less place to talk about, that's the Nsextensioncontext class's play pause event. When do we need to call it?
After my research, this can be used as follows
- (void)mediaplay{
NSLog(@"Mediaplay, start playing");
//Click the play button to pause playback after 4s
dispatch_after(dispatch_time(dispatch_time_now, (int64_t) (4.0 * nsec_per_sec)), dispatch_get_main_queue(), ^{
[self. Extensioncontext mediaplayingpaused];
});
}
- (void)mediapause{
NSLog(@"mediapause, pause Playback");
//Click the pause button to start playback after 10s
dispatch_after(dispatch_time(dispatch_time_now, (int64_t) ( 10.0 * nsec_per_sec)), dispatch_get_main_queue(), ^{
[self. Extensioncontext mediaplayingstarted];
});
}
There's a few points to note here.
1. In this controller, we can directly electrophoresis Self.extensioncontext to invoke its playback pause method.
2. Call this playback pause method, and do not recall-(void) mediaplay{} or-(void) mediapause{}, only a simple invocation. After my tests,
- (void)mediaplayingstarted{
NSLog(@"active call starting Method");
}
- (void)mediaplayingpaused
{
NSLog(@"active call to Pause method");
}
Using the above code, is not listening to the method of clicking the Yo ~
With the custom content, naturally there is a custom action (behavior), I will make up tomorrow ~ Thank you everyone ~
If you like my article, don't forget to pay attention to me, thank you all ~
In addition, if you want to reprint, hope can indicate the source, I will write more and better articles, to give back to everyone ~
Demo Address
Https://github.com/CoderWQ/XZPushTest/tree/master
IOS10 push must see Unnotificationcontentextension