1.
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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 | import UIKit @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { regisigerNotification() let tv = UITextView(frame: CGRect(x: 0, y: UIScreen.main.bounds.size.height * 0.7, width: 300, height: 300)) tv.backgroundColor = UIColor.cyan //FIXME: 手动杀死 APP,在进入,这里接收不到本地通知的bug。 //FIXME:操作行为在iOS12上, X系列没用 // tv.text = launchOptions?.description // print("launchOptions?.description = \(launchOptions?.description)") tv.textColor = UIColor.red window?.rootViewController?.view.addSubview(tv) if launchOptions != nil{ if let lacal = launchOptions?[UIApplication.LaunchOptionsKey.localNotification]{ //用户点击本地通知 启动APP : 真是开发,做点击本地通知的业务处理 print( "lacal = \(lacal)" ) } } //目前使用这个方法可以接收到本地通知的信息 tv.text = UIApplication.shared.scheduledLocalNotifications?.description print( "UIApplication.shared.scheduledLocalNotifications?.description = \(UIApplication.shared.scheduledLocalNotifications?.description)" ) // let str = UIApplication.shared.scheduledLocalNotifications?.description // let jsonData = str?.utf8 // if let loacl = UIApplication.shared.scheduledLocalNotifications?.description as [UIApplication.LaunchOptionsKey : Any]?{ // // } return true } //进入前台:清空角标 func applicationDidBecomeActive(_ application: UIApplication) { UIApplication.shared.applicationIconBadgeNumber = 0 } func application(_ application: UIApplication, handleActionWithIdentifier identifier: String?, for notification: UILocalNotification, completionHandler: @escaping () -> Void) { print( "dsadsadsadsa" ) completionHandler() } //接受本地通知 func application(_ application: UIApplication, didReceive notification: UILocalNotification) { print( "接受到通知" ) let sw = UISwitch() window?.rootViewController?.view.addSubview(sw) } //注册本地通知 private func regisigerNotification(){ // //简单方式实现 // if #available(iOS 8.0, *) { // let uns = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil) // UIApplication.shared.registerUserNotificationSettings(uns) // } //复杂方式实现 //1.请求本地权限 let type = UIUserNotificationType.alert.rawValue | UIUserNotificationType.badge.rawValue | UIUserNotificationType.sound.rawValue //FIXME:操作行为在iOS12上, X系列没用 //FIXME:操作行为在iOS12上, X系列没用 //FIXME:操作行为在iOS12上, X系列没用 //创建一组操作行为 let categorie1 : UIMutableUserNotificationCategory = UIMutableUserNotificationCategory() /// 设置组标识 categorie1.identifier = "selected" //设置组里面的操作行为1 let action1 = UIMutableUserNotificationAction() //设置操作行为的参数 action1.identifier = "操作1" action1.title = "标题1" // action1.behavior /// 用户的点击动作前台还是在后台 action1.activationMode = .foreground //前台解锁: 如果在前台的话这个属性会被忽略 action1.isAuthenticationRequired = true /// 是否是破坏性行为(使用红色表示,表示这个按钮) action1.isDestructive = true //设置组里面的操作行为2 let action2 = UIMutableUserNotificationAction() //设置操作行为的参数 action2.identifier = "操作2" action2.title = "标题2" // action1.behavior if #available(iOS 9.0, *){ action1.behavior = .textInput action1.parameters = [UIUserNotificationTextInputActionButtonTitleKey: "修改的标题" ] } /// 用户的点击动作前台还是在后台 action2.activationMode = .background //前台解锁: 如果在前台的话这个属性会被忽略 action2.isAuthenticationRequired = false /// 是否是破坏性行为(使用红色表示,表示这个按钮) action2.isDestructive = false let actions = [action1, action2] //设置组里面的操作行为 // 如果针对于弹框样式的通知 // default 代表, 最多可以显示4个按钮 // minimal, 代表,最多可以显示2个按钮 categorie1.setActions( actions, for : UIUserNotificationActionContext.minimal) //2.附加操作行为 let categories : Set<UIUserNotificationCategory> = [categorie1] //设置对象 let sets = UIUserNotificationSettings(types: UIUserNotificationType(rawValue: type), categories: categories) //注册通知设置 UIApplication.shared.registerUserNotificationSettings(sets) } } |
2.VC里面
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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | import UIKit class ViewController: UIViewController { @IBAction func sendNotification(_ sender: Any) { /// 创建 let localNotification = UILocalNotification() //设置标题 if #available(iOS 8.2, *) { localNotification.alertTitle = "斗地主卡卡" } //此处的 category 和 AppDelegate里面设置的要一样 localNotification.category = "selected" //设置内容 localNotification.alertBody = "通知来了" //几秒之后执行 localNotification.fireDate = Date(timeIntervalSinceNow: 2) //声音 不起作用 // localNotification.soundName = UILocalNotificationDefaultSoundName localNotification.soundName = "lose.caf" //重复周期:最少1分钟 localNotification.repeatInterval = .minute //锁屏文字 下面两行配合使用 localNotification.alertAction = "打开666应用" localNotification.hasAction = true //启动图片(当用户点了本地通知,d启动我们APP的时候,带的启动图片) //FIXME:但是在iOS9之后这个属性 不起作用。。 //FIXME:但是在iOS9之后这个属性 不起作用。。 localNotification.alertLaunchImage = "2.jpg" // 应用程序图标右上角显示的消息数 localNotification.applicationIconBadgeNumber = 3 // 通知上绑定的其他信息,为键值对 localNotification.userInfo = [ "id" : "1" , "name" : "xxxx" ] //立即发送 // UIApplication.shared.presentLocalNotificationNow(localNotification) //发送:按照设置的执行时间发送 UIApplication.shared.scheduleLocalNotification(localNotification) } //取消 @IBAction func cancleNotification(_ sender: Any) { UIApplication.shared.cancelAllLocalNotifications() } //查看 @IBAction func viewNotification(_ sender: Any) { print(UIApplication.shared.scheduledLocalNotifications) } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现