Transferred from: http://www.cocoachina.com/ios/20160316/15665.html
Objective
Push notification, presumably everyone is very familiar with, about the principle and the like, here is not too much elaboration. Here we mainly introduce the next iOS8 and iOS9 about the new features of the push. You may have seen it, but some friends may not have done it. This article will give you a detailed introduction to push the shortcut buttons and quick reply to the implementation of such functions.
Quick likes, like Weibo features
We can first look at the following:
First of all, how to add a shortcut to your own push, the action in the demo "like", the code is as follows:
Create a message above the action you want to add uimutableusernotificationaction *action1 = [[ uimutableusernotificationaction alloc] init]; action1.identifier = knotificationactionidentifilestar; action1.title = @ "Likes"; //when clicked does not start the program, in the background processing action1.activationMode = The uiusernotificationactivationmodebackground; //needs to be unlocked to process (meaning that if you receive a notification on the lock screen and the iphone has a screen lock set, Click likes will not go directly to our callback for processing, but need to enter the screen lock password before entering our callback), if action.activationmode = Uiusernotificationactivationmodeforeground; the attribute is ignored; action1.authenticationrequired After the = yes; /* destructive property is set, left in the notification bar or lock screen, The button color changes to red if all two buttons are set to Yes, they are all red (slightly unsightly) if the two buttons are set to No, the default value , the first is blue, the second is light gray if a yes is no, the corresponding color is displayed, that is, the red-blue two-color (CP color). */ action1.destructive = no;
About the meaning of the parameter, the above code is already explained in detail, then the action button is created, we need to create an action set, and register, the code is as follows:
//Create a Category collection of actions (buttons) uimutableusernotificationcategory *category = [[UIMutableUserNotificationCategory alloc] init]; //the only indication of this set of actions (Knotificationcategoryidentifile is a macro defined for me, which can be defined by itself) category.identifier = knotificationcategoryidentifile; //supports up to two, if more words are added, the following will be ignored [category setactions:@[action1, action2] forcontext: ( Uiusernotificationactioncontextminimal)]; //Create uiusernotificationsettings and set the display class type of the message UIUserNotificationSettings *uns = [UIUserNotificationSettings Settingsfortypes: (uiusernotificationtypealert| uiusernotificationtypebadge| Uiusernotificationtypesound) categories:[nsset setwithobject:category]]; [[ Uiapplication sharedapplication] registerusernotificationsettings:uns];
In this case, we can realize the function of quick and easy push. Of course, we will also notice the SMS in the iphone has a quick reply function, this is how to achieve it. Sir please keep looking down.
Quick reply, such as SMS drop-down shortcut reply function
The usual, or the first, as follows:
The action of this comment and the "likes" above are just one more attribute setting, i.e. "behavior". The code is as follows:
The second action uimutableusernotificationaction *action2 = [[Uimutableusernotificationaction alloc] init]; Action2.identifier = knotificationactionidentifilecomment; Action2.title = @ "comment"; When the click does not start the program, in the background processing action2.activationmode = Uiusernotificationactivationmodebackground; If the Behavior property is set to Uiusernotificationactionbehaviortextinput, then clicking the button will show the input box for input action2.behavior = Uiusernotificationactionbehaviortextinput;
Careful friend may notice me in the quick reply input content, the input box right button name and the text message's quick reply button name is not the same (the text message "sends", the demo is "the comment"), this button we also can make the customization, the code is as follows:
This dictionary defines the name of the button on the right side of the input box when the Comment button is clicked, and if the dictionary is not set, the right button name defaults to "send" action2.parameters = @{ Uiusernotificationtextinputactionbuttontitlekey: @ "comment"};
Here, the basic function of the demo is almost fully realized. The rest is the callback, the callback function is appdelegate in the proxy method, as follows:
Local notification callback function, called when the application is in the foreground-(void) Application: (UIApplication *) application didreceivelocalnotification: ( Uilocalnotification *) notification;//received the local message in the non-app interface, the drop-down message will have a quick Reply button, click the button after the method called, according to identifier to determine which button to click, Notification for message Content-(void) Application: (UIApplication *) application Handleactionwithidentifier: (Nullable NSString *) Identifier forlocalnotification: (uilocalnotification *) notification Withresponseinfo: (nsdictionary *) responseinfo Completionhandler: (void (^) ()) Completionhandler;
Conclusion
1: The above code is only registered local push, remote push and local push similar, push message if you want to trigger the shortcut action, you need to include the above category in the message ontology identifier flag. Such as
The need for this "identifile" is consistent with the knotificationcategoryidentifile you set in the code {"APS": {"Identifile": "Test Remote Notification", " Sound ":" Default "," badge ": 1," category ":" Category "}}
2: In code code when you need to be compatible with the lower version, please pay attention to the version of the judgment.
3: Finally, the demo address.
IOS Play push Notifications