Swift push local push (uilocalnotification)

Source: Internet
Author: User

Local push notifications are implemented by instantiating Uilocalnotification. To implement localization push can add code implementation in Appdelegate.swift, this case is a message to the user when the app enters the background.

1. First add code within the Didfinishlaunchingwithoptions method, IOS8 push the message first to obtain the consent of the user, the initial installation of the app will prompt the user whether to allow the program to push messages, this method is the first time the app is run, is executed once, The method is not executed each time it is activated from the background.

Func application (application:uiapplication, didfinishlaunchingwithoptions launchoptions: [Nsobject:anyobject]?)- > Bool {        if (Uidevice.currentdevice () systemversion as NSString). Floatvalue >= 8 {            // Apservice.registerforremotenotificationtypes (                //uiusernotificationtype.badge.rawvalue |                UIUserNotificationType.Sound.rawValue |                UIUserNotificationType.Alert.rawValue,                //categories:nil)                        Application.registerusernotificationsettings (Uiusernotificationsettings (fortypes:                Uiusernotificationtype.badge |                Uiusernotificationtype.sound |                Uiusernotificationtype.alert, Categories:nil)                }                apservice.setupwithoption (launchoptions)        return True    }

2. There are several ways to say it,

1.func applicationwillresignactive (application:uiapplication) {} This event will be triggered when the app will enter the background, lock screen, and call in

2.func Applicationdidenterbackground (application:uiapplication) {} Triggers this event when the app enters the background

3.func Applicationwillenterforeground (application:uiapplication) {} Triggers this event when the app is coming back to the foreground from the background

4.func applicationdidbecomeactive (application:uiapplication) {} Triggers this event when the app becomes active

5.func applicationwillterminate (application:uiapplication) {} This method is triggered when the app exits, and is typically used to save certain data

At this point, write the following code within 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.soundname = UilocalnoTificationdefaultsoundname//setting app ' s icon badge notification.applicationiconbadgenumber = 1 var userinfo:[nsobject:anyobject] = [Nsobject:anyobject] () userinfo["Klocalnotificationid"] = "LocalNotifi Cationid "userinfo[" key "] =" Attention please "notification.userinfo = UserInfo//uiapplicati On.sharedapplication (). schedulelocalnotification (Notification)//uiapplication.sharedapplication (). Presentlocalnotificationnow (notification) Application.presentlocalnotificationnow (notification)}

When you press the home key to switch the app to the background there will be a push message, and the app's corner mark becomes "1"

3. When the user clicks on the message, it triggers the Didreceivelocalnotification event and writes some code in the event:

Func application (application:uiapplication, Didreceivelocalnotification notification:uilocalnotification) {        Uiapplication.sharedapplication (). Cancelalllocalnotifications () let        userInfo = notification.userinfo!        Let title = userinfo["Key"] as! String                var alert = Uialertview ()        alert.title = title        alert.message = Notification.alertbody        Alert.addbuttonwithtitle (notification.alertaction!)        Alert.cancelbuttonindex = 0        alert.show ()                    //apservice.showlocalnotificationatfront (notification, Identifierkey:nil)    }

4. Remove the icon's corner mark when the program is active

Func applicationdidbecomeactive (application:uiapplication) {        //Restart Any tasks this were paused (or not yet Starte D) While the application is inactive. If the application is previously in the background, optionally refresh the user interface.        Setting the desk top application icon ' s badge as zero        //application.applicationiconbadgenumber = 0        Application . Cancelalllocalnotifications ()        application.applicationiconbadgenumber = 0            }

Swift push local push (uilocalnotification)

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.