Swift Local notification, remote notification

Source: Internet
Author: User

Both remote push and local push require a registration notification code

IOS 8 divides the registration of the original one step remotenotification into two parts, one for registering the newly introduced "uiusernotificationsettings", and the other for remotenotifications.

In fact, Apple has unified remotenotification and localnotification in IOS 8. The two notifications will be unified by Uiusernotificationsettings to manage the user interface related things: tags, sounds, and reminders.  In addition to the unified user interface notification, Uiusernotificationsettings also introduces Uiusernotificationcategory, which allows users to easily perform some quick actions directly on Notification (action )。

if (uiapplication instancesrespondtoselector ("Selector (" Registerusernotificationsettings: "))) {

Application. Registerusernotificationsettings (Uiusernotificationsettings (Fortypes:uiusernotificationtype.  Sound | Uiusernotificationtype.  Alert | Uiusernotificationtype. Badge, Categories:nil))

} else {

Application. Registerforremotenotificationtypes (. Alert |. Sound |. Badge)

}

Then, write related code such as remote push, local notification, etc. in Appdelegate.swift

Receive local notifications

Func application (application:uiapplication, didreceivelocalnotification notification:uilocalnotification) {

var Alertview = Uialertview (title: "System Local Notification", Message:notification. Alertbody, Delegate:nil, Cancelbuttontitle: "Back")

Alertview. Show ()

}

//Remote push notification registration successful

Func application (application:uiapplication, Didregisterforremotenotificationswithdevicetoken devicetoken:nsdata) {

println (devicetoken. Description)

}

Remote Push notification registration failed

Func application (application:uiapplication, Didfailtoregisterforremotenotificationswitherror error:nserror) {

If error. Code = = 3010 {

println ("Push Notifications is not supported in the IOS Simulator.")

} else {

println ("Application:didfailtoregisterforremotenotificationswitherror:/(Error)")

}

}

Remote push notification received before 8.0

Func application (application:uiapplication, didreceiveremotenotification userInfo: [Nsobject:anyobject]) {

Let Notif = UserInfo as Nsdictionary

Let Apsdic = Notif. Objectforkey ("APS") as Nsdictionary

Let Alertdic = Apsdic. Objectforkey ("alert") as String

var Alertview = Uialertview (title: "System Local Notification", Message:alertdic, Delegate:nil, Cancelbuttontitle: "Back")

Alertview. Show ()

}

Receive remote push notifications after 8.0

Func application (application:uiapplication, didreceiveremotenotification userInfo: [Nsobject:anyobject], FetchCompl Etionhandler Completionhandler: (Uibackgroundfetchresult), Void) {

Let Notif = UserInfo as Nsdictionary

Let Apsdic = Notif. Objectforkey ("APS") as Nsdictionary

Let Alertdic = Apsdic. Objectforkey ("alert") as String

var Alertview = Uialertview (title: "Remote Push Notification", Message:alertdic, Delegate:nil, Cancelbuttontitle: "Back")

Alertview. Show ()

}

Registration Notice alert, sound, badge (after 8.0, you must add the following code, or registration fails)

Func application (application:uiapplication, didregisterusernotificationsettings notificationsettings: Uiusernotificationsettings) {

Application. Registerforremotenotifications ()

}

Local Notification Method ********** * * * * * ***************

//

Timeviewcontroller.swift

Uicontroldemo

//

Created by on 14/12/10.

Copyright (c) 2014 careless. All rights reserved.

//

Import UIKit

Class Timeviewcontroller:baseviewcontroller {

var Wordtextfield:uitextfield? Text

var Datetextfield:uitextfield? Time

var datepicker:uidatepicker?

Override Func Viewdidload () {

Super. Viewdidload ()

Self. title = "Time/date/local notification"

Let Leftbarbutton:uibarbuttonitem = Uibarbuttonitem (barbuttonsystemitem:. Trash, target:self, Action: "Locanotifcationdeleteall")

Self. Navigationitem. Rightbarbuttonitem = Leftbarbutton;

Wordtextfield = Uitextfield (Frame:cgrectmake (50, 80, 200, 40))

Wordtextfield?. BackgroundColor =. Clearcolor ()

Wordtextfield!. Tag = 100

Wordtextfield?. BorderStyle = Uitextborderstyle. Roundedrect

Wordtextfield?. Keyboardtype = Uikeyboardtype. Default

Wordtextfield?. Returnkeytype = Uireturnkeytype. Done

Wordtextfield?. Contentverticalalignment = uicontrolcontentverticalalignment. Center

Wordtextfield?. Clearbuttonmode = Uitextfieldviewmode. Whileediting

Wordtextfield?. Securetextentry = False

Wordtextfield?. TextColor =. Blackcolor ()

Wordtextfield?. TextAlignment =. Left

Wordtextfield?. placeholder = "Keyboard"

Wordtextfield?. Font = Uifont. Systemfontofsize (15)

Self. View. Addsubview (Wordtextfield!)

Datetextfield = Uitextfield (Frame:cgrectmake (50, 140, 200, 40))

Datetextfield?. BackgroundColor =. Clearcolor ()

Datetextfield!. Tag = 101

Datetextfield?. BorderStyle = Uitextborderstyle. Roundedrect

Datetextfield?. Keyboardtype = Uikeyboardtype. Default

Datetextfield?. Returnkeytype = Uireturnkeytype. Done

Datetextfield?. Contentverticalalignment = uicontrolcontentverticalalignment. Center

Datetextfield?. TextColor =. Blackcolor ()

Datetextfield?. TextAlignment =. Left

Datetextfield?. placeholder = "Date picker"

Datetextfield?. Font = Uifont. Systemfontofsize (15)

Self. View. Addsubview (Datetextfield!)

DatePicker = Uidatepicker ()

DatePicker?. Datepickermode =. DateAndTime

DatePicker?. TimeZone = Nstimezone. Systemtimezone ()

DatePicker?. AddTarget (Self, Action: "datepickerselected:", forcontrolevents:uicontrolevents. valuechanged)

Datetextfield!. Inputview = DatePicker

var button = UIButton. Buttonwithtype (Uibuttontype. Custom) as UIButton

button. frame = CGRectMake (200, 200, 50, 30)

button. BackgroundColor = Uicolor. Redcolor ()

button. Settitlecolor (Uicolor. Blackcolor (), forstate:. Normal)

button. Settitle ("Save", Forstate:uicontrolstate. Normal)

button. Titlelabel!. Font = Uifont. Boldsystemfontofsize (CGFloat (20))

button. Showstouchwhenhighlighted = True

button. AddTarget (Self, Action: "Savelocalnotificationbutton", forcontrolevents:uicontrolevents. Touchupinside)

Self. View. Addsubview (Button)

}

Save Button method

Func Savelocalnotificationbutton () {

var contentdic = ["KEY": "VALUE"]

Locanotifcationschedule (Cheduldate:datepicker!. Date, Alertbody: "Notifications See:/(Wordtextfield!.) Text) ", Content:contentdic)

var Alertview = Uialertview (title: "Saved successfully", Message:nil, Delegate:nil, Cancelbuttontitle: "Back")

Alertview. Show ()

Wordtextfield?. Resignfirstresponder ()

Datetextfield?. Resignfirstresponder ()

}

Register for local Notifications

Func locanotifcationschedule (#chedulDate: NSDate, alertbody:string, content:nsdictionary) {

var localnotif = uilocalnotification ()

Localnotif. Firedate = Cheduldate

Localnotif. TimeZone = Nstimezone. Defaulttimezone ()

Localnotif.repeatinterval = repeatinterval 0 means no repetition

Localnotif. Soundname = "IPHONE.CAF"//This property can not be written (default system sound uilocalnotificationdefaultsoundname)

Localnotif.hasaction = true;

Localnotif.alertaction = "View";

Localnotif. Alertbody = Alertbody

Localnotif. UserInfo = Content

UIApplication. Sharedapplication (). Schedulelocalnotification (Localnotif)

}

Delete all registered local notifications

Func Locanotifcationdeleteall () {

Let application = UIApplication. Sharedapplication ()

Application. Cancelalllocalnotifications ()

var Alertview = Uialertview (title: "All local notifications have been removed", Message:nil, Delegate:nil, Cancelbuttontitle: "Back")

Alertview. Show ()

}

Dynamically Change TextField content

Func datepickerselected (datepicker:uidatepicker) {

Let datestring = Timedateformatter (). Stringfromdate (datePicker. Date)

Datetextfield!. Text = datestring

}

Override Func touchesended (Touches:nsset, withevent event:uievent) {

Wordtextfield?. Resignfirstresponder ()

Datetextfield?. Resignfirstresponder ()

}

Override Func didreceivememorywarning () {

Super. Didreceivememorywarning ()

}

}

Swift Local notification, remote notification

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.