1.
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, 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里面
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) } }