复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50// // ViewController.swift // Dome2test // // Created by 郭文亮 on 2018/11/22. // Copyright © 2018年 finalliang. All rights reserved. // import UIKit //用户通知类库 import UserNotifications //对于函数 类和协议。可以使用@available声明这些类型的生命周期,依赖于特定的平台货或者操作系统版本 @available(iOS 10.0,*) //使用当前的的视图控制器类 尊魂用户通知中心协议。 用于处理接受的通知 以及通知相关错左操作 class ViewController: UIViewController ,UNUserNotificationCenterDelegate{ override func viewDidLoad() { super.viewDidLoad() //创建一个本地通知对象 let localNotification = UILocalNotification() //创建一个基于当前时间的日期对象 let now = Date() //设置在当前时间的五秒后 触发本地通知 localNotification.fireDate = now.addingTimeInterval(5) //然后设置本地通知的重复次数 localNotification.repeatInterval = NSCalendar.Unit.init(rawValue: 0) //设置本地通知的时区为默认时区 localNotification.timeZone = .current //设置本地通知的提醒声音模版 localNotification.soundName = UILocalNotificationDefaultSoundName //设置本地通知的信息内容 localNotification.alertBody = "Hi, it's time to make a decision!" //在程序图标的右上角位置,显示当前通知的数量 localNotification.applicationIconBadgeNumber = 1 //创建一个字典对象 用来在通知中传递数据 let infoDic = NSDictionary(object: "message.", forKey: "infoKey" as NSCopying) //将字典对象 作为本地通知的用户信息 localNotification.userInfo = infoDic as [NSObject : AnyObject] //开始执行本地通知 UIApplication.shared.scheduleLocalNotification(localNotification) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } }
在程序的AppDelegate中修改代码
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { if #available(iOS 8.0, *) { //创建一个用户通知配置对象 let settings = UIUserNotificationSettings(types: [.alert,.badge,.sound], categories: nil) //对应用程序 ,注册用户通知的配置信息 application.registerUserNotificationSettings(settings) } return true } func applicationWillResignActive(_ application: UIApplication) { application.applicationIconBadgeNumber -= 1 } func application(_ application: UIApplication, didReceive notification: UILocalNotification) { //获得通知对象的用户信息 并转换成字典对象 let dic = NSDictionary(dictionary: notification.userInfo!) //根据键名获得字典对象对应的值 let value = dic["infoKey"] print("The value of infoKey is : (value)") //将程序右上角的数字-1 application.applicationIconBadgeNumber -= 1 }
最后
以上就是聪慧唇膏最近收集整理的关于IOS实用功能-系统本地通知的创建的全部内容,更多相关IOS实用功能-系统本地通知内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复