zoukankan      html  css  js  c++  java
  • [Xcode 实际操作]九、实用进阶-(11)系统本地通知的创建和使用

    目录:[Swift]Xcode实际操作

    本文将演示系统本地通知的创建和使用。

    在项目导航区,打开视图控制器的代码文件【ViewController.swift】

     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】

     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 }

    运行程序,点击【确定】按钮,允许程序使用设备的通知功能。

  • 相关阅读:
    一次线上bug引起的反思
    本地调试接口返回信息不对 以及 jar冲突问题
    404问题记录
    Intelij IDEA 配置Tomcat时找不到 “Application Server”
    java多线程处理问题
    DataTemplate和ControlTemplate的关系
    WP模板
    wp中的动画
    wp中的位图
    2013.7.22-7.28开发资料汇总
  • 原文地址:https://www.cnblogs.com/strengthen/p/10088988.html
Copyright © 2011-2022 走看看