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. html 自定义属性的获取和应用

    在html 中,我们可以给元素设置自定义属性,格式:  data-属性="属性值",可以设置一个,也可以设置多个 1.获取元素属性的常规方法:直接获取元素,然后使用getAttri ...

  2. 尚硅谷springboot学习3-helloworld程序

    1.环境准备 –jdk1.8:Spring Boot 推荐jdk1.7及以上:java version "1.8.0_112" –maven3.x:maven 3.3以上版本:Ap ...

  3. 尚硅谷redis学习6-持久化RDB

    是什么 持久化文件保存在dump.rdb中 持久化策略 在shutdown或flush或flushall后会立即持久化 重新启动后会从rdb文件中恢复数据 可以手动持久化 持久化失败时不允许写,如在强 ...

  4. HashMap 实现总结

    Entry类中需包含键值的hash值,防止resize时的重复计算: Map容量为2的整幂,可使用位操作取代取余操作提高效率: resize时需要将原table的桶内数据置null,利于垃圾回收: h ...

  5. python进行爬虫

    使用python进行网络爬虫 非结构画数据 转为 结构化数据.需要借助ETL(数据抽取,转换,存储)进行. 非结构化数据蕴含着丰富的价值.需要借助ETL进行转换成结构化数据,才能变成有价值的数据.比如 ...

  6. mysql查询记录修改时间于现在大于30分钟

    (unix_timestamp(now())-`updatetime`)>1800

  7. OpenCV批量读入(处理)

    #include <windows.h> #include <iostream> #include <opencv2/opencv.hpp> using names ...

  8. Java的学习01

    记录每天的学习情况.加油. /** * 测试包装类 * @author 小白 * */ public class TestWrappedClass { public static void main( ...

  9. Java的学习路线图

    在网上看到一个关于Java的学习路线图,个人感觉很详细.https://blog.csdn.net/s1547823103/article/details/79768938

  10. R各种数据类型的转换

    1.列表转化为数据框 df <- data.frame(matrix(unlist(列表), nrow=132, byrow=T),stringsAsFactors=FALSE)