IOS10 push must see UNNotificationContentExtension

Everybody is waiting! ~ this is absolutely the most complete, the most detailed UNNotificationContentExtension explain yo ~!

In this article, I will give you the next part of the push content, I hope you can read this article, you can easily play and push.

1, UNNotificationContentExtension

1.1 introduction of UNNotificationContentExtension

In simple terms, the UNNotificationContentExtension class is iOS10 push new features to send, the official document with such a sentence, simple explanation, Presents a custom interface for a delivered local or remote notification. (when you receive the remote or local notification, a custom pop-up interface). The effect is shown in figure

IOS10 push must see UNNotificationContentExtension
IMG_2451.PNG-91kB

and the following figure

IOS10 push must see UNNotificationContentExtension
QQ20161014-0.png-43.4kB

Figure two
above is what we are going to make the effect, in the custom View area, you can put on a video, put on a calendar, put on a show the location of the Label, in short, we can customize the View!

1.2, how to build a new UNNotificationContentExtension

Creating the UNNotificationContentExtension of this Target is similar to creating UNNotificationServiceExtension steps.
step 1: creating

IOS10 push must see UNNotificationContentExtension
image

second step: select Create type

IOS10 push must see UNNotificationContentExtension
QQ20161014-1.png-82.6kB

You can then see that
is generated

IOS10 push must see UNNotificationContentExtension
QQ20161014-2.png-22.2kB

1.3, how to use and related Demo

When it comes to how to use this class, we must first say, “what are the documents that the system generates for us?”.

IOS10 push must see UNNotificationContentExtension
QQ20161014-2.png-22.2kB

> 0.Info.plist

IOS10 push must see UNNotificationContentExtension
QQ20161014-3.png-102.4kB

The following
NSExtensionAttributes in this dictionary, we have three attributes can be added a
.UNNotificationExtensionCategory.
(there must be, system has been created)
explained: corresponding to the value of the key can be a string, can also be an array, each string is a identifier, the corresponding identifier the attributes of each UNMutableNotificationContent categoryIdentifier.

In simple terms, is in receipt of the notice, we can let the server send this notice with categoryIdentifier, is, we can according to the video, music, pictures, to inform the content of our custom respectively. Different classification identifiers also help us distinguish between what types of notifications we use when we talk about UNNotificationAction, and make it easier for us to do different kinds of behavior for different types of notifications. In the screenshot above, I’m in the form of a string. The following is the array form:

IOS10 push must see UNNotificationContentExtension
QQ20161014-4.png-39.9kB

When using, we refer to the following code:

This method is UNNotificationServiceExtension / / the 1. class, I can refer to an article about UNNotificationServiceExtension, I need to rewrite my notice here. - (void) didReceiveNotificationRequest: (UNNotificationRequest * request) (withContentHandler: (void ^) (UNNotificationContent * _Nonnull) contentHandler) {self.contentHandler = contentHandler; / / copy notice to start doing 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 heading"; "I am @ self.bestAttemptContent.subtitle = self.bestAttemptContent.body = @ sub title"; "different from Xu"; / / NSDictionary annex *dict = self.bestAttemptContent.userInfo; NSDictionary *not IDict dict[@ = "APS"]; NSString *imgUrl = [NSString stringWithFormat:@ notiDict[@, "% @" "imageAbsoluteString" ";!!!!! Here's the point!!!!!!!!!!!! "I write here dead category1, in fact, receive push, each push the best content with an catagory server, with a good agreement, so that it is convenient for us according to categoryIdentifier from the definition of different types of views, as well as the action @ self.bestAttemptContent.categoryIdentifier =" category1 ";}

Everybody attention, above

!!!!! Here's the point!!!!!!!!!!!! "I write here dead category1, in fact, receive push, each push the best content with an catagory server, with a good agreement, so that it is convenient for us according to categoryIdentifier from the definition of different types of views, and action = self.bestAttemptContent.categoryIdentifier @ category1";

Set categoryIdentifier here, it is best to let the server push the content with this, and then we are better customization. Local writing is not recommended.
can refer to my demo address

Two.UNNotificationExtensionInitialContentSizeRatio.
(must have already created a system,
) explanation: this value type is a floating-point types, represents the ratio of height and width. This ratio is used by the system as the size of the initial view. For a simple example, if the value is 1, the view is square. If 0.5, the height is half of the width.

Note that this value is initialized to a value in this expansion after adding can override the frame, when the show, we have not open this view preview, the background is a similar picture for the gray, the grey height width ratio, is through the value to set.

Three.UNNotificationExtensionDefaultContentHidden.

(optional) explanation: this value is a BOOL value, when YES, the content view will hide the top of the original push, we will only display a custom view. (because in time, we can obtain a custom view push content, then we want to show the layout, if) NO (default is NO), push the view will have a custom view we, will push the content view with original system (annex is not shown here. Body will only display the text inside).

Four. As for NSExtensionMainStoryboard and NSExtensionPointIdentifier, the system generated by default, we directly use it, if you need to change, can only change the use of the storyboard name (but nobody should be put in the establishment of a system to remove O (a _ U) O)

> 1.MainInterface.storyboard

This is a simple storyboard file with a View inside, and this View is the custom View view in the layer above. It is bound to NotificationViewController.

> 2.NotificationViewController

This is the system to help us default to create a controller, inherit UIViewController, in fact, is a controller. “We need to explain a method for this controller,”
said:

  1. To comply with the UNNotificationContentExtension protocol, we need to use the method
This method is / / said, as long as you receive the notice, and to ensure the setting of categoryIdentifier, set with info.plist inside, you can call this method. Note: multiple notifications of a session can be called when each notification is received. - - (void) didReceiveNotification: (UNNotification *) notification;

Use as follows:

- (void) didReceiveNotification: (UNNotification * notification) {this method can give their own control assignment ah, ah frame adjustment and so on, I'm here to print out the contents of the notice, for everyone to use. NSDictionary *dict = notification.request.content.userInfo; / / here you can put everything out of print NSLog (@ "% @", dict); /**************************** print information stand APS = {alert = "This is some fancy message.; badge = 1; from =" Hello, I'm Xu imageAbsoluteString = "http://upload.univs.cn/2012/0104/1325645511371.jpg"; ";" mutable-content "= 1; sound = Default;};}} *******************************************/

Here, the simple UNNotificationContentExtension is done, and I’m going to add something. In UNNotificationContentExtension.h, there is such an enumeration

Typedef NS_ENUM (NSUInteger, UNNotificationContentExtensionMediaPlayPauseButtonType) {/ / UNNotificationContentExtensionMediaPlayPauseButtonTypeNone / / no play button, a play button, click the play button after the switch UNNotificationContentExtensionMediaPlayPauseButtonTypeDefault is still there, the music is similar, / / play button, click the play button, disappear, click again to pause button after the recovery of UNNotificationContentExtensionMediaPlayPauseButtonTypeOverlay,}

See such enumeration, we must wonder how to use ah. Please see the following properties

Set the playback button / attribute @property (nonatomic, readonly, assign) UNNotificationContentExtensionMediaPlayPauseButtonType mediaPlayPauseButtonType; / / set the playback button frame @property (nonatomic, readonly, assign) CGRect mediaPlayPauseButtonFrame; / / set the playback button color @property (nonatomic, readonly, copy) UIColor *mediaPlayPauseButtonTintColor; / / began to pause - (void) - (mediaPlay; void) mediaPause;

There are the following classes, this class although there is a way to end the play began to play with, but be careful, this is NSExtensionContext, and the method above we are talking about the methods of the UNNotificationContentExtension protocol in. Everybody should pay attention.

@interface NSExtensionContext (UNNotificationContentExtension) / (void) - control mediaPlayingStarted / / control pause (void) mediaPlayingPaused @end

See these properties and want to know how to use them. See my steps below:

Analysis:
, first of all, these attributes are readonly, so the direct use of the self. attribute to modify, must be wrong, so we can use only the get method.
secondly: according to the type of button, we can relate that if button does not, the method of starting the pause is useless. If there is button, naturally we have the operation of the play, the legend of other UI space, we came to have to rewrite its frame, to determine his position. Sets the color to set the display color of it. Set the type of button so that he can show it.

Returns the default style / / button - (UNNotificationContentExtensionMediaPlayPauseButtonType) mediaPlayPauseButtonType {return UNNotificationContentExtensionMediaPlayPauseButtonTypeDefault;} / / return button frame - (CGRect) mediaPlayPauseButtonFrame {return CGRectMake (100, 100, 100, 100);} / / return button color (UIColor *) mediaPlayPauseButtonTintColor{return [UIColor blueColor];}

Through the code above, our button has been shown. The following diagram (please ignore the following policy button):

IOS10 push must see UNNotificationContentExtension
IMG_2475.PNG-115.4kB

specific location, you can rewrite the frame to determine the location of button.

When we click on the blue button, we can perform some play pause, as follows

- - (void) mediaPlay{NSLog (@ mediaPlay, start playing ');} - - (void) mediaPause{NSLog (@ mediaPause, pause play);}

There’s one more place to say, and that’s the play pause for the NSExtensionContext class. When do we need to call?
after my research, you can use this as follows

- (void) mediaPlay{NSLog (@ mediaPlay, began to play "); / / click the play button, 4S pause dispatch_after (dispatch_time (DISPATCH_TIME_NOW (int64_t) (4 * NSEC_PER_SEC)), dispatch_get_main_queue (), ^{[self.extensionContext mediaPlayingPaused];});} - (void) mediaPause{NSLog (@" mediaPause, pause "); / / click the pause button, 10s began to play dispatch_after (dispatch_time (DISPATCH_TIME_NOW (int64_t) (10 * 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 tap self.extensionContext to call its play pause method.
2. calls this play pause method, and does not call again – (void) mediaPlay{} or – (void) mediaPause{}, and can only be called simply. After my test,

- - (void) mediaPlayingStarted{NSLog (@ method of active call initiation);} - - (void) mediaPlayingPaused {NSLog (@ active calling pause method);}

With the above code, is not listening to the click of the method yo ~!

The custom content, naturally have a custom action (behavior), I will fill ~ thank you ~
if you love my article, don’t forget about me, thank you ~
and if you want to reprint, to indicate the source, I will write more articles to thank everyone ~

The important thing is to say three times, demo address,
important thing, say three times, demo address,
important thing, say three times, demo address