Swift-nsnotification notification for iPhone Development

Source: Internet
Author: User
Tags notification center

Swift notification Creation

  1. Create a singleview Application
  2. Open appdelegate. Swift and go to

Application (Application: uiapplication, didfinishlaunchingwitexceptions launchoptions: nsdictionary ?)

Input code:

Func application (Application: uiapplication, didfinishlaunchingwitexceptions launchoptions: nsdictionary ?) -> Bool {

// Set the Notification Type

Let types: uiusernotificationtype = uiusernotificationtype. Alert | uiusernotificationtype. Badge

// Set the notification setting item. The categories parameter is used to set the Notification category.

Let mysettings: uiusernotificationsettings = uiusernotificationsettings (fortype: types, categories: nil );

// Register usernotification

Uiapplication. sharedapplication (). registerusernotifiationsettings (mysettings)

Return true

}

  1. Configure different actions in the Method

Application (Application: uiapplication, didfinishlaunchingwitexceptions launchoptions: nsdictionary ?) Enter the following code

Func application (Application: uiapplication, didfinishlaunchingwitexceptions launchoptions: nsdictionary ?) -> Bool {

// Define actions

VaR firstaction: uimutableusernotificationaction = uimutableusernotificationaction ();

// The unique identifier for this action

Fistaction. identifier = "first_action ";

// The localized title to display for this action

Firstaction. Title = "First Action ";

// Define action's activationmode, // how the application shocould be activated in response to the action

Firstaction. activationmode = uiusernotifactivactivationmode. Background // when clicked, the program is not started and processed in the background.

// Define action's destructive // whether this action shocould be indicated as destructive when displayed.

Firstaction. destructive = true

// Define authentication // whether this action is secure and shocould require unlocking before being med. If the activation mode is locked, then the action is considered secure and this property is ignored.

Firstaction. authenticationrequired = false // You can process the notification without unlocking your phone.

 

VaR secondaction: uimutableusernotificationaction = uimutableusernotificationaction ()

Secondaction. identifier = "second_action ";

Secondaction. Title = "Second Action ";

Secondaction. activationmode = uiusernotifactivactivationmode. Foreground

Secondaction. destructive = false

Secondaction. authenticationrequired = false

 

VaR thirdaction: uimutableusernotificationaction = uimutableusernotificationaction ()

Thirdaction. identifier = "third_action ";

Thirdaction. Title = "third action ";

Thirdaction. activationmode = uiusernotifactivactivationmode. Background

Thirdaction. destructive = false

Thirdaction. authenticationrequired = false

 

// Category

VaR firstcategory: uimutableusernotificationcategory = uimutableusernotificationcategory ()

Firstcategory. identifier = "first_category ";

 

Let defaactiactions: nsarray = [firstaction, secondaction, thirdaction];

Let minimalactions: nsarray = [firstaction, secondaction];

 

// Sets the uiusernotifactiactions in the order to be displayed for the specified context

Firstcategory. setactions (defaultactions, forcontext: uiusernotificationactioncontext. Default); // The default context of a notification action

Firstcategory. setactions (minimalactions, forcontext: uiusernotifactioncontext. Minimal); // minimal // The context of a notification action when space is limited

 

Let categories: nsset = nsset (Objects: firstcategory)

 

 

// Set the Notification Type

Let types: uiusernotificationtype = uiusernotificationtype. Alert | uiusernotificationtype. Badge

// Set the notification setting item. The categories parameter is used to set the Notification category.

Let mysettings: uiusernotificationsettings = uiusernotificationsettings (fortype: types, categories: nil );

// Register usernotification

Uiapplication. sharedapplication (). registerusernotifiationsettings (mysettings)

Return true

}

 

4. Open the viewcontroller. Swift file and enter the following code in the viewdidload method:

Override func viewdidload (){

Super. viewdidload ()

// Define notification center

Nsnotificationcenter. defacenter Center (). addobserver (self, selector: "testshape:", name: "actionone", object: nil)

Nsnotificationcenter. defacenter Center (). addobserver (self, selector: "testmessage:", name: "actiontwo", object: nil)

// Define a program that triggers notification
VaR datecomp: nsdatecomponents = nsdatecomponents ()

Datecomp. Year = 2014

Datecomp. month = 09

Datecomp. Day = 09

Datecomp. Hour = 11

Datecomp. Minute = 11

Datecomp. timezone = nstimezone. systemtimezone ()

 

VaR calendar: nscalendar = nscalendar (calendaridentifier: nsgregoriancalendar)

VaR Date: nsdate = calendar. datefromcomponents (datecomp)

 

 

// Define location notification

VaR notification: uilocalnotification = uilocalnotification ()

Notification. Category = "first_category ";

Notification. alertbody = "This is a notification"

Notification. firedate = Date

 

// Fire notification

Uiapplication. Invoke application (). schedulelocalnotification (notification)

}

 

Func testshape (Notification: nsnotification ){

Uiview * view = uiview (frame: cgrectmake (100,100,100,100 ));

View. backgroundcolor = uicolor. blackcolor ()

Self. View. addsubview (view)

}

 

Func testmessage (Notification: nsnotification ){

VaR message: uialertcontroller = uialertcontroller (title: "notification message", message: "Hello, this is an alert message", preferredstyle: uialertcontrollerstyle. Alert)

Message. addaction (uialertaction (title: "OK", style: uialertactionstyle. Default, handle: nil ))

 

Self. presentviewcontroller (message, animated: True, completion: nil)

}

 

5. Return to appdelegate. Swift and add the following method:

Funcapplication (Application: uiapplication !, Handleactionwithidentifier identifier: String !, Forlocalnotification notification: uilocalnotification !, Completionhandler: ()-> void )!) {

If identifier = "first_action "{

Nsicationicationcenter. defacenter Center (). postnotificationname ("actionone", object: nil)

}

Else if identifier = "second_action "{

Nsicationicationcenter. defacenter Center (). postnotificationname ("actiontwo", object: nil)

}

Completionhandler ()

}

 

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.