[Xcode 實際操做]9、實用進階-(11)系統本地通知的建立和使用

目錄:[Swift]Xcode實際操做html

本文將演示系統本地通知的建立和使用。swift

在項目導航區,打開視圖控制器的代碼文件【ViewController.swift】app

 1 import UIKit
 2 //引入須要使用的用戶通知類庫
 3 import UserNotifications
 4 
 5 //對於函數、類和協議,
 6 //能夠使用@available聲明這些類型的生命週期,依賴於特定的平臺或者操做系統的版本。
 7 @available(iOS 10.0, *)
 8 //添加一個用戶通知中心協議UNUserNotificationCenterDelegate,
 9 //使當前的視圖控制器類,遵循UNUserNotificationCenterDelegate。
10 //該協議用於處理接收到的通知,以及和通知相關的全部操做。
11 class ViewController: UIViewController, UNUserNotificationCenterDelegate {
12     
13     override func viewDidLoad() {
14         super.viewDidLoad()
15         // Do any additional setup after loading the view, typically from a nib.
16         //建立一個本地通知對象
17         let localNotification = UILocalNotification()
18         
19         //建立一個基於當前時間的日期對象
20         let now = Date()
21         //設置在當前時間的5秒以後,觸發本地通知
22         localNotification.fireDate = now.addingTimeInterval(5)
23         
24         //設置本地通知的重複次數
25         localNotification.repeatInterval = NSCalendar.Unit.init(rawValue: 0)
26         //設置本地通知的時區爲默認時區
27         localNotification.timeZone = .current
28         //設置本地通知的提醒聲音的模板
29         localNotification.soundName = UILocalNotificationDefaultSoundName
30         //設置本地通知的信息內容
31         localNotification.alertBody = "Hi, My name is strengthen!"
32         //在程序圖標的右上角位置顯示通知的數量
33         localNotification.applicationIconBadgeNumber = 1
34         
35         //建立一個字典對象,用來在通知中傳遞數據
36         let infoDic = NSDictionary(object: "message.", forKey: "infoKey" as NSCopying)
37         //將字典對象,做爲本地通知的用戶信息
38         localNotification.userInfo = infoDic as [NSObject : AnyObject]
39         
40         //開始執行本地通知
41         UIApplication.shared.scheduleLocalNotification(localNotification)
42     }
43     
44     override func didReceiveMemoryWarning() {
45         super.didReceiveMemoryWarning()
46         // Dispose of any resources that can be recreated.
47     }  
48 }

點擊切換項目代理文件【AppDelegate.swift】ide

 1 import UIKit
 2 
 3 @UIApplicationMain
 4 class AppDelegate: UIResponder, UIApplicationDelegate {
 5 
 6     var window: UIWindow?
 7 
 8 
 9     func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
10         // Override point for customization after application launch.
11         
12         //編輯應用程序進入的方法
13 
14         //判斷設備的系統版本是否大於11
15         if #available(iOS 12.0, *)
16         {
17             //若是系統版本大於11
18             //建立一個用戶通知配置對象
19             let settings = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
20             //對應用程序,註冊用戶通知的配置信息
21             application.registerUserNotificationSettings(settings)
22         }
23         
24         return true
25     }
26 
27     func applicationWillResignActive(_ application: UIApplication) {
28         // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
29         // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
30         //在應用程序即將退出時,將程序圖標右上角的數字減1
31         application.applicationIconBadgeNumber -= 1
32     }
33     
34     //添加一個代理方法,用來響應接收到通知的事件
35     func application(_ application: UIApplication, didReceive notification: UILocalNotification) {
36         //得到通知對象的用戶信息,並轉換爲字典對象
37         let dic = NSDictionary(dictionary: notification.userInfo!)
38         //根據鍵名,得到字典對象對應的值
39         let value = dic["infoKey"]
40         //在控制檯打印輸出相關日誌
41         print("The value of infoKey is: \(String(describing: value))")
42         //同時將程序圖標右上角的數字減1
43         application.applicationIconBadgeNumber -= 1
44     }
45     
46     func applicationDidEnterBackground(_ application: UIApplication) {
47         // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
48         // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
49     }
50 
51     func applicationWillEnterForeground(_ application: UIApplication) {
52         // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
53     }
54 
55     func applicationDidBecomeActive(_ application: UIApplication) {
56         // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
57     }
58 
59     func applicationWillTerminate(_ application: UIApplication) {
60         // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
61     }
62 }

運行程序,點擊【肯定】按鈕,容許程序使用設備的通知功能。函數

相關文章
相關標籤/搜索