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, 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里面

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)
} }

  

swift - 本地通知2 - 啰嗦版的更多相关文章

  1. swift - 本地通知

    1. AppDelegate  注册 class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? fun ...

  2. ios开发——实用技术OC-Swift篇&本地通知与远程通知详解

    本地通知与远程通知详解 一:本地通知   Local Notification的作用 Local Notification(本地通知) :是根据本机状态做出的通知行为,因此,凡是仅需依赖本机状态即可判 ...

  3. 如何在 iOS 8 中使用 Swift 实现本地通知(上)

    当你的应用在后台运行时,可以简单地使用本地通知把信息呈现给用户.它可以允许你显示 提醒.播放提示音和数字角标(badge).本地通知可以被以下的事件触发:计划好的时间点或者用户进入和离开某个地理区域. ...

  4. Swift - 本地消息的推送通知(附样例)

    使用UILocalNotification可以很方便的实现消息的推送功能.我们可以设置这个消息的推送时间,推送内容等. 当推送时间一到,不管用户在桌面还是其他应用中,屏幕上方会都显示出推送消息. 1, ...

  5. 如何在 iOS 8 中使用 Swift 实现本地通知(下)

    在上集中,我们已经构建了一个简单的待办列表应用(to-do list app),这个应用可以在待办项过期时通过本地通知提醒用户.现在,我们要在之前的基础上添加以下功能:应用图标角标上显示过期待办项的数 ...

  6. Swift 本地推送通知UILocalNotification

    Notification是智能手机应用开发中常用的信息传递机制,它不用消耗更多资源去不停的检查信息状态,可以非常好的节省资源. 在iOS中分为两种通知:本地.远程.本地的UILocalNotifica ...

  7. iOS8无法弹出本地通知?

    最近在看<iOS编程(第4版)>(就是Big Nerd Ranch用的那本教材).这本书写的不错,推荐一下,写的很细致,循序渐进,不能不赞一下外国人写书的思路,确实跟国人不同.之前学And ...

  8. [Xcode 实际操作]九、实用进阶-(11)系统本地通知的创建和使用

    目录:[Swift]Xcode实际操作 本文将演示系统本地通知的创建和使用. 在项目导航区,打开视图控制器的代码文件[ViewController.swift] import UIKit //引入需要 ...

  9. UILocalNotification本地通知的使用方法

    本文所写方法主要应用UILocalNotification达到本地推送通知栏信息 取消了其他教程里过期的UIAlertView方法 使用UILocalNotification主要分为创建 调用 取消 ...

随机推荐

  1. 认识JavaWeb,servlet, JSP, Tomcat, http协议,Web服务器

    JavaWeb通常指服务器端的Java应用开发. 一般来说,服务器是在网络通信条件下工作的,这就离不开http协议. HTTP协议,是为服务器和客户端通信提供的规范,其中规定了信息的格式,符合规范格式 ...

  2. sql中优化查询

    1.在大部分情况下,where条件语句中包含or.not,SQL将不使用索引:可以用in代替or,用比较运算符!=代替not. 2.在没有必要显示不重复运行时,不使用distinct关键字,避免增加处 ...

  3. UML类图实例分析

    登录模块 某基于C/S的即时聊天系统登录模块功能描述如下: 用户通过登录界面(LoginForm)输入账号和密码,系统将输入的账号和密码与存储在数据库(User)表中的用户信息进行比较,验证用户输入是 ...

  4. American Football Vocabulary!

    American Football Vocabulary! Share Tweet Share You’ll learn all about the vocabulary of American fo ...

  5. 【378】python any() and all()

    Reference: [1] Python all() - Python Standard Library [2] Python any() - Python Standard Library all ...

  6. avalon2学习教程01

    经过难苦奋战,avalon2终于面世了.这花了大半年时间,其中1.6还胎死腹中.长达半年没有产出,我都担心自己会被裁掉…… avalon2许多API与1.4.×保持一致,当然也添加了一些1.5的功能, ...

  7. 如何遍历Set对象

    对 set 的遍历 1.迭代遍历: Set<String> set = new HashSet<String>(); Iterator<String> it = s ...

  8. Hibernate 再接触 树状结构设计以及学生课程成绩表的设计

    1 树状结构的设计 package com.bjsxt.hibernate; import java.util.HashSet; import java.util.Set; import javax. ...

  9. winform下利用webBrowser执行javascript

    目前很多网站为了防止恶意提交表单信息,大多都采用了加密的方式对提交信息进行处理,加密处理后通过POST提交给服务器验证,这种操作一般都是用Javascipt进行加密,若是我们想要正确提交表单到网站,就 ...

  10. servlet中url-pattern之/与/*的区别