Swift Local Push notification uilocalnotification

Source: Internet
Author: User

Notificationis a common information transmission mechanism in the development of smartphone application, it can save resources very well without consuming more resources to check the information status constantly.

There are two types of notifications in iOS: local, remote. The local uilocalnotification is managed globally NotificationManager , and we only need to add the local notification object to the system's notification queue, and the system will fire local notifications at the specified time.

Local Push notification: uilocalnotification
    1. If you want to use push notifications, you must first register the types of notifications you want to use in Apple's push notification service, such as the following snippet of code that registers reminders, tags, and sounds at the same time (iOS 8 doesn't require registration):
// 在appDelegate中注册通知func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { if #available(iOS 8.0, *) { let uns = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil) UIApplication.sharedApplication().registerUserNotificationSettings(uns) }}

2. Create and add local notifications

Class Localnotificationutils:nsobject {/** add create and add local notification */class Func addnotification () {Initialize a notification let Localnoti = Uilocalnotification ()The trigger time of the notification, such as 15 minutes immediately after let Firedate = NSDate (). Datebyaddingtimeinterval (-15*Localnoti). firedate = FiredateSet the time zone Localnoti. TimeZone = Nstimezone.defaulttimezone ()Topic content displayed on the notification Localnoti.alertbody =  "notifications" .soundname = Uilocalnotificationdefaultsoundname Span class= "Hljs-comment" >//the sliding action tip of the Standby interface Localnoti.alertaction = " open app "//application icon The number of messages displayed in the upper right corner localnoti0 //notification of additional information bound to the key value pair localNoti< Span class= "hljs-selector-class" >.userinfo = [ "id":  "1",  "name":  "xxxx"] //add notifications to the system queue, The system will trigger Uiapplication.sharedapplication () at the specified time. schedulelocalnotification (Localnoti)}}    

3. Get all local notifications

let locals = UIApplication.sharedApplication().scheduledLocalNotifications

4. Cancel a local push

    Cancel the notification by notifying the ID that is bound by the notification, where the ID is also the information you stored in the UserInfo class func deletenotification (id:string) {if OrderID. IsEmpty {return} if let locals = Uiapplication.sharedapplication (). s cheduledlocalnotifications {for Localnoti with locals { if let dict = Localnoti. userInfo { if dict. Keys. Contains ("id") && dict["id"] is String && (dict["id"] as! String) = = id { //Cancel Notification uiapplication.sharedapplication (). Cancellocalnotification (Localnoti)}}}} 
                   

5. Cancel all local notifications

UIApplication.sharedApplication().cancelAllLocalNotifications()

6. Click the trigger event after notification

1. The application is running (running in foreground or background), after clicking the notification Trigger Appdelegate Agent method:: didreceivelocalnotification

class AppDelegate: UIResponder, UIApplicationDelegate{ /** 接收本地通知 */ func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) { // 获取通知上绑定的信息 guard let dict = notification.userInfo else { return } // 后面作相应处理... }}

2. App not running, click Notifications to launch the app, Go appdelegate Agent method: Didfinishlaunchingwithoptions

ClassAppdelegate:Uiresponder,uiapplicationdelegate{Funcapplication (Application:uiapplication, Didfinishlaunchingwithoptions launchoptions: [Nsobject:anyobject]?) -bool {//omit create window and root controller code ... //here only describes how to get notified when a click Notification launches the app if launchoptions! = nil {if let localnotification = launchOptions![  "Uiapplicationlaunchoptionslocalnotificationkey"] as? uilocalnotification {if let dict = Localnotification.userinfo {//get the information bound by the notification after the corresponding processing ...}} return true}}        

Reference: Http://www.cnblogs.com/kenshincui/p/4168532.html#localNotification

Swift Local Push notification 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.