iOS10-swift实现本地消息
- 首先在AppDelegate.swift里注册通知,在App首次启动时会提示用户是否接受此App通知.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {application.registerUserNotificationSettings(UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil))return true
}
- 直接贴代码. 这里的示例是一个表视图,点击表视图里的某项,就会在某个时间点以后弹出本地消息给用户.
var requestIdentifier = "" //请求标识符,取消消息时要用到 override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){print("You selected cell #\(indexPath.row)!")let cell = tableView.cellForRow(at: indexPath) //勾选的行//计算出目标时间点距离现在的时间差
let iStory = array[indexPath.row] as! Jiemu
let title = iStory.JiemuName as! String
let toTime = iStory.Time//这就是目标时间点
let dateForm = DateFormatter()
dateForm.dateFormat = "yyyy-MM-dd HH:mm"
let da = dateForm.date(from: toTime as! String)
let second = da?.timeIntervalSince(NSDate() as Date) //计算出时间差
let xxjj = Int(second!) //取整数值,忽略小数点// 1. 创建通知内容
let content = UNMutableNotificationContent()
content.title = "这是通知标题."
content.body = "这是通知正文内容"// 测试用的,注释掉了,这句表示30秒以后发送通知.
//vartrigger = UNTimeIntervalNotificationTrigger(timeInterval: 30, repeats: false)//时间差大于1秒才进入正确逻辑
if xxjj > 1 {//如果当前项已被勾选,再次点击会取消通知.
if cell?.accessoryType == .checkmark {cell?.accessoryType = .none//取消提醒
UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: [self.requestIdentifier] )} else {//标记勾选,弹出提示框,并创建消息请求.cell?.accessoryType = .checkmarkaddTips() //弹出提示框// 2. 创建发送触发
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: TimeInterval(xxjj), repeats: false)
// 3. 发送请求标识符,取消消息时要用到
requestIdentifier = toTime as! String + " " + title// 4. 创建一个发送请求
let request = UNNotificationRequest(identifier: requestIdentifier, content: content, trigger: trigger)// 将请求添加到发送中心
UNUserNotificationCenter.current().add(request) { error in
if error == nil {
print("Time Interval Notification scheduled: \(self.requestIdentifier)")
}
}}} else {
//时间差小于1的逻辑处理:
let alertController = UIAlertController(title: "系统提示",
message: "时间已过.", preferredStyle: .alert)let okAction = UIAlertAction(title: "OK", style: .default, handler: nil)
alertController.addAction(okAction)self.present(alertController, animated: true, completion: nil)}}//提示框
func addTips(){let alertController = UIAlertController(title: "系统提示",
message: "已设置提醒", preferredStyle: .alert)
// let cancelAction = UIAlertAction(title: "取消", style: .cancel, handler: nil)let okAction = UIAlertAction(title: "OK", style: .default, handler: {
action in
print("点击ok后在,执行这里的代码")})
alertController.addAction(okAction)self.present(alertController, animated: true, completion: nil)}
【iOS10-swift实现本地消息】参考资料:
1
2
2
推荐阅读
- 关于QueryWrapper|关于QueryWrapper,实现MybatisPlus多表关联查询方式
- MybatisPlus使用queryWrapper如何实现复杂查询
- python学习之|python学习之 实现QQ自动发送消息
- 孩子不是实现父母欲望的工具——林哈夫
- opencv|opencv C++模板匹配的简单实现
- Node.js中readline模块实现终端输入
- java中如何实现重建二叉树
- 人脸识别|【人脸识别系列】| 实现自动化妆
- paddle|动手从头实现LSTM
- pytorch|使用pytorch从头实现多层LSTM