Swift push local push (uilocalnotification) with the two-button message

Source: Internet
Author: User

The previous article about the local push is a common message push, this article to talk about the push message with the button action, the last picture to see:

Following the previous article to make a small change:

Make the following modifications within the Didfinishlaunchingwithoptions method

if (Uidevice.currentdevice (). Systemversion as NSString). Floatvalue >= 8 {//Apservice.registerforremotenotif Icationtypes (//UIUserNotificationType.Badge.rawValue |//UIUserNotificationType.Sound.rawVa                        Lue |//uiusernotificationtype.alert.rawvalue,//categories:setting.categories) 1. Create a set of actions var useraction = uimutableusernotificationaction () Useraction.identifier = "A ction "Useraction.title =" Accept "Useraction.activationmode = Uiusernotificationactivationmode.fore Ground var userAction2 = uimutableusernotificationaction () useraction2.identifier = "act Ion2 "Useraction2.title =" Ingore "Useraction2.activationmode = Uiusernotificationactivationmode.bac                        Kground useraction2.authenticationrequired = True useraction2.destructive = True       2. Create a category collection of actions     var usercategory = uimutableusernotificationcategory () usercategory.identifier = "Mynotification" Usercategory.setactions ([Useraction,useraction2], ForContext:UIUserNotificationActionContext.Minimal) var c            Ategories:nsset = Nsset (object:usercategory)//3. Create Uiusernotificationsettings and set the display class type of the message                    var usersetting = uiusernotificationsettings (ForTypes:UIUserNotificationType.Badge |                    Uiusernotificationtype.sound | Uiusernotificationtype.alert, categories:categories as set<nsobject>)//4. Register Push Application.registerforremotenotifications () application.registerusernotificationsettings (userSet Ting)}

 2. Modify the Applicationdidenterbackground method

Func Applicationdidenterbackground (application:uiapplication) {//Use the method to release shared resources, SA  ve user data, invalidate timers, and store enough application state information to restore your application        State-in-case it is terminated later. If your application supports background execution, this method is called instead of Applicationwillterminate:when the        User quits.        Uiapplication.sharedapplication (). Cancelalllocalnotifications () var notification = Uilocalnotification () Notification.firedate = NSDate (). Datebyaddingtimeinterval (1)//setting TimeZone as Localtimezone Noti        Fication.timezone = Nstimezone.localtimezone () Notification.repeatinterval = Nscalendarunit.calendarunitday         Notification.alerttitle = "This is a local notification" notification.alertbody = "Hey,it's great to see you Again" Notification.alertaction = "OK" notification.category = "mynotification "//This is very important, with the above action set (uimutableusernotificationcategory) identifier like notification.soundname = Uilocalnotificati  Ondefaultsoundname//setting app ' s icon badge notification.applicationiconbadgenumber = 1 var Userinfo:[nsobject:anyobject] = [Nsobject:anyobject] () userinfo["Klocalnotificationid"] = "LocalNotificationID "userinfo[" key "] =" Attention "Notification.userinfo = UserInfo//uiapplication.share Dapplication (). schedulelocalnotification (Notification)//uiapplication.sharedapplication (). Presentlocalnotificationnow (notification) Application.presentlocalnotificationnow (notification)}

3. Click the Push message button will trigger Func application (application:uiapplication, Handleactionwithidentifier identifier:string?, Forlocalnotification notification:uilocalnotification, Completionhandler: (), Void) {} This method.

If it is a remote push that is the Func application (application:uiapplication, Handleactionwithidentifier identifier:string?, Forremotenotification userInfo: [Nsobject:anyobject], Completionhandler: (), Void) {} This method.

You only need to call the local first method to

Func application (application:uiapplication, Handleactionwithidentifier identifier:string?, forLocalNotification Notification:uilocalnotification, Completionhandler: ()-Void) {        println ("identifier=\ (identifier)")  / /The identifier here is the button's identifier                Completionhandler ()  ///Finally, make sure to call this method    }

Swift push local push (uilocalnotification) with the two-button message

Related Article

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.