美文网首页iOS Developer
iOS10-swift实现本地消息

iOS10-swift实现本地消息

作者: yytester | 来源:发表于2017-01-03 17:21 被阅读32次
  1. 首先在AppDelegate.swift里注册通知,在App首次启动时会提示用户是否接受此App通知.
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        
            application.registerUserNotificationSettings(UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil))

        return true
    }
  1. 直接贴代码. 这里的示例是一个表视图,点击表视图里的某项,就会在某个时间点以后弹出本地消息给用户.
 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秒以后发送通知.          
          //var  trigger = 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 = .checkmark

                    addTips() //弹出提示框
                    
                    // 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)

    }

参考资料:
1
2
2

相关文章

  • iOS10-swift实现本地消息

    首先在AppDelegate.swift里注册通知,在App首次启动时会提示用户是否接受此App通知. 直接贴代码...

  • 消息中间件

    消息中间件,也可以叫做中央消息队列或者是消息队列(区别于本地消息队列,本地消息队列指的是 JVM 内实现的队列实现...

  • 本地套接字

    本地套接字是 IPC,也就是本地进程间通信的一种实现方式。除了本地套接字以外,其它技术,诸如管道、共享消息队列等也...

  • iOS 10:本地消息推送及实现

    文章结构: 前言 这阶段在学习有关 iOS 10 消息推送的内容,通过查阅资料并亲身实...

  • 本地消息推送

    注册本地通知 - (BOOL)application:(UIApplication *)application d...

  • 本地消息提醒

    -(void)LocalNotification{ if([[[UIDevicecurrentDevice]sys...

  • NSPointArray弱的使用 消息转发

    通过FMDB 写了一个本地 即使通讯录和消息存储的数据库,想实现如果当本地数据库数据更新的时候,通知其他相关UI同...

  • 分布式事务-最终一致性

    基于事务消息实现最终一致性。 通过MQ事务消息能力。 假设两个本地事务,对应服务A和服务B。 服务A: 发送一条事...

  • 分布式事务解决方案

    分布式事务的实现主要有以下 5 种方案: XA 方案 TCC 方案 本地消息表 可靠消息最终一致性方案 最大努力通...

  • APNS

    在iOS开发中,消息推送有两种形式,一种是本地推送,一种是远程推送。 本地推送相对来说比较简单,不要要联网就可实现...

网友评论

    本文标题:iOS10-swift实现本地消息

    本文链接:https://www.haomeiwen.com/subject/ypdnvttx.html