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. 用URL传递参数

    用URL传递参数,在园子里找到一篇文章解决了自己的问题,地址如下:http://www.cnblogs.com/lolicon/archive/2009/01/19/1378408.html

  2. iOS 坐标转换

    例:把A view上的某个点的坐标(a)转换到B view上,两种方法 CGPoint targetPointB = [A convertPoint:a toView:B];(记忆方法:把A上的某个点 ...

  3. vue ...mapMutations 的第一个参数默认为 数据对象state

    1.实现回调后 路由的跳转 mutationsLoginHeaderBackFun(state,$router) { console.log(state); console.log($router); ...

  4. 解决 'Could not convert variant of type (NULL) into type (String)

    写存储过程中有不允许为空的字段,在客户端转化取数时显示 Could not convert variant of type (NULL) into type (String) 可以在存储过程中使用is ...

  5. numpy-Randow

    Randow使用 http://blog.csdn.net/pipisorry/article/details/39508417 概率相关使用 转:http://www.cnblogs.com/Nau ...

  6. easyui分页,根据网友的一段代码优化了一下

    千言万语尽在代码中,可以自己看,不清楚留言吧! <%@ Page Language="C#" AutoEventWireup="true" CodeBeh ...

  7. mysql-5.5.20预编译安装

    1.MYSQL数据库概念 1)MYSQL是一款关系型数据库系统,数据之间有互相联系,互相的关联和调用的. 2)MYSQL数据用于存储:WEB网站用户名和密码等 3)MYSQL存储数据库是通过二维表格形 ...

  8. tensorflow中run和eval的区别(转)

    在tensorflow中,eval和run都是获取当前结点的值的一种方式. 在使用eval时,若有一个 t 是Tensor对象,调用t.eval()相当于调用sess.run(t) 一下两段代码等效: ...

  9. Realtime Rendering 6

    [Realtime Rendering 6] 1.Lighting computations occur in two phases: 1)light phase. used to compute t ...

  10. SpringBoot点滴(1)

    spring boot 注意事项 1.项目启动的主类,放置位置在所有类的外层与controller,dao,service,util,entity同层,SpringBoot会自动扫描@SpringBo ...