ios--Local Notification Quick Reply method

Source: Internet
Author: User

Apple after IOS8 has a brand-new notification mechanism, when we receive the push message, or lock screen, let the user quickly operation of the notification, the implementation of the following:

Here's how we can achieve this effect:

First, add a button on the page, click the button to send a local notification,

UIButton *btn = [UIButton buttonwithtype:uibuttontypecustom];     = CGRectMake (+ +);     = [Uicolor yellowcolor];    [Self.view addsubview:btn];        [Btn addtarget:self Action: @selector (btnact:) forcontrolevents:uicontroleventtouchupinside];    

Button Implementation method:

- (void) Btnact: (UIButton *) btn{//1. Create the action to be added on the message, displayed as a button//1.1 Accept ButtonUimutableusernotificationaction *acceptaction = [uimutableusernotificationactionNew]; Acceptaction.identifier=@"acceptaction";//Add IdentityAcceptaction.title =@"Accept";//set the text displayed on the buttonAcceptaction.activationmode = Uiusernotificationactivationmodeforeground;//start the program when clicked//1.2 Reject buttonUimutableusernotificationaction *rejectaction = [uimutableusernotificationactionNew]; Rejectaction.identifier=@"rejectaction"; Rejectaction.title=@"Reject"; Rejectaction.activationmode= Uiusernotificationactivationmodebackground;//do not start the program when clickedrejectaction.authenticationrequired = YES;//need to be unlocked to process if Rejectaction.activationmode = Uiusernotificationactivationmodeforeground; then this property will be ignoredRejectaction.destructive = YES;//whether the button event is irreversible//2. Create a category collection of actionsUimutableusernotificationcategory *categorys = [uimutableusernotificationcategoryNew]; Categorys.identifier=@"Alert";//identity of the action collection[Categorys setactions:@[acceptaction, Rejectaction] Forcontext:uiusernotificationactioncontextminima L]; //add two buttons to the collection of actions and set the context style//3. Create a uiusernotificationsettings and set the display type of the messageUiusernotificationsettings *settings = [uiusernotificationsettings settingsfortypes: (UIUserNotificationTypeAlert | Uiusernotificationtypebadge |uiusernotificationtypesound) Categories:[nsset Setwithobjects:categorys, Nil]]; //4. Registration Notice[[UIApplication sharedapplication] registerforremotenotifications];                [[UIApplication sharedapplication] registerusernotificationsettings:settings]; //we use local notifications as an example of how to display the notification barUilocalnotification *notification = [uilocalnotificationNew]; if(Notification! =Nil) {Notification.firedate= [NSDate Datewithtimeintervalsincenow:2];//Trigger TimeNotification.alertaction =@"alertaction";//define action names for viewing notificationsNotification.hasaction =YES; Notification.alertbody=@"Test Push Quick reply (the body content of the notification: Wuge most handsome)"; Notification.soundname= Uilocalnotificationdefaultsoundname;//The background sound of the notificationNotification.applicationiconbadgenumber =1;//OCD Most hated icons the top left corner that little numberNotification.category =@"Alert"; Notification.userinfo= @{@"Key1":@"value1",@"Key2":@"value2"};//to bind notifications with some additional information needed to process notifications//show notification messages based on the configuration of the triggering event[[UIApplication sharedapplication] schedulelocalnotification:notification]; //send notifications to your phone now//[[UIApplication sharedapplication] presentlocalnotificationnow:notification];     }                }

Then we need to do something else in the Appledelegate protocol method:

//after a successful registration notice, there will be a callback method, written in Appdelegate#pragmaMark-This method is called after a successful registration notification-(void) Application: (UIApplication *) application didregisterusernotificationsettings: (Uiusernotificationsettings *) notificationsettings{//successfully registered Registerusernotificationsetting: After the callback methodNSLog (@"successfully registered%@", notificationsettings);}#pragmaMark-the method that was called after the local push was received-(void) Application: (UIApplication *) application didreceivelocalnotification: (Uilocalnotification *) notification{//method called after local message is receivedNSLog (@"method called after a local message is received%@", notification); //the number on the icon minus 1Application.applicationiconbadgenumber-=1;}#pragmaMark-Notifies the callback method of event handling-(void) Application: (UIApplication *) application Handleactionwithidentifier: (NSString *) identifier forlocalnotification: ( Uilocalnotification *) Notification Completionhandler: (void(^) ()) completionhandler{//in the non-app page received the local message, the drop-down message will have a quick Reply button, click on the method called, according to identifier to determine which button clicked, notification for the message contentNSLog (@"%@ --- %@", identifier, notification); if([identifier isequaltostring:@"acceptaction"]) {NSLog (@"Click on the Receive button"); Firstviewcontroller*FIRSTVC =[[Firstviewcontroller alloc]init]; Uinavigationcontroller*NAVC =[[Uinavigationcontroller ALLOC]INITWITHROOTVIEWCONTROLLER:FIRSTVC]; Self.window.rootViewController=NAVC; }Else{NSLog (@"Click the Reject button"); }                //After you finish processing the message, make sure to call this code block at the endCompletionhandler ();}

This is the realization of ...

ios--Local Notification Quick Reply method

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.