zoukankan      html  css  js  c++  java
  • Swift 本地推送通知UILocalNotification

    Notification是智能手机应用开发中常用的信息传递机制,它不用消耗更多资源去不停的检查信息状态,可以非常好的节省资源。

    在iOS中分为两种通知:本地、远程。本地的UILocalNotification由全局的NotificationManager统一管理,我们只需要将本地通知对象添加到系统的Notification队列中就可以了,系统会在指定的时间激发本地通知。

    本地推送通知:UILocalNotification

    1. 如果要使用推送通知,必须先在苹果的推送通知服务里注册你要使用哪几种类型的通知,就比如下面的一段代码就表示同时注册了提醒、标记和声音两种类型的通知(ios 8之前是不需要注册的):
    // 在appDelegate中注册通知
    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
            if #available(iOS 8.0, *) {
                let uns = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)
                UIApplication.sharedApplication().registerUserNotificationSettings(uns)
            }
    }

    2.创建并添加本地通知

    class LocalNotificationUtils: NSObject {
    
         /** 添加创建并添加本地通知 */
        class func addNotification() {
             // 初始化一个通知
             let localNoti = UILocalNotification()
    
            // 通知的触发时间,例如即刻起15分钟后
             let fireDate = NSDate().dateByAddingTimeInterval(-15*60) 
             localNoti.fireDate = fireDate
             // 设置时区
             localNoti.timeZone = NSTimeZone.defaultTimeZone()
             // 通知上显示的主题内容
             localNoti.alertBody = "通知上显示的提示内容"
             // 收到通知时播放的声音,默认消息声音
             localNoti.soundName = UILocalNotificationDefaultSoundName
             //待机界面的滑动动作提示
             localNoti.alertAction = "打开应用"
             // 应用程序图标右上角显示的消息数
             localNoti.applicationIconBadgeNumber = 0
             // 通知上绑定的其他信息,为键值对
             localNoti.userInfo = ["id": "1",  "name": "xxxx"]
    
            // 添加通知到系统队列中,系统会在指定的时间触发       
             UIApplication.sharedApplication().scheduleLocalNotification(localNoti)
        }
    
    }

    3.获取所有本地通知

    let locals = UIApplication.sharedApplication().scheduledLocalNotifications

    4.取消一个本地推送

        // 通过通知上绑定的id来取消通知,其中id也是你在userInfo中存储的信息
        class func deleteNotification(id: String) {
            if orderID.isEmpty {
                return
            }
    
            if let locals = UIApplication.sharedApplication().scheduledLocalNotifications {
                for localNoti in locals {
                    if let dict = localNoti.userInfo {
    
                        if dict.keys.contains("id") && dict["id"] is String && (dict["id"] as! String) == id {
                            // 取消通知
                            UIApplication.sharedApplication().cancelLocalNotification(localNoti)
                        }
                    }
                }
            }
        }

    5.取消所有本地通知

    UIApplication.sharedApplication().cancelAllLocalNotifications()

    6.点击通知后的触发事件

    1.应用在正在运行(在前台或后台运行),点击通知后触发appDelegate代理方法::didReceiveLocalNotification

    class AppDelegate: UIResponder, UIApplicationDelegate{
    
        /** 接收本地通知 */
        func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) {
            // 获取通知上绑定的信息
            guard let dict = notification.userInfo else {
                return
            }
    
            // 后面作相应处理...
    
        }
    }

    2.应用未运行,点击通知启动app,走appDelegate代理方法:didFinishLaunchingWithOptions

    class AppDelegate: UIResponder, UIApplicationDelegate{
        func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    
            // 省略创建window及根控制器等代码
            ......
            // 此处只介绍当点击通知启动应用后如何获取通知
            if launchOptions != nil {
                if let localNotification = launchOptions!["UIApplicationLaunchOptionsLocalNotificationKey"] as? UILocalNotification {
                    if let dict = localNotification.userInfo {
                            // 获取通知上绑定的信息后作相应处理...
                    }
                }
            }
    
    
            return true
    
       }
    }

    参考:http://www.cnblogs.com/kenshincui/p/4168532.html#localNotification

  • 相关阅读:
    nginx配置反向代理
    hyperchain HVM使用java编写智能合约的编译、部署流程
    leetcode 140单词拆分Ⅱ
    bomblab phase5
    bomb lab 二三阶段
    2021暑假算法学习笔记(基础复习)#2
    2021暑假算法学习笔记(基础复习)#1
    O(logn)最长上升子序列并输出
    A Daily Topic # 7 阶乘的和(二进制/枚举)
    A Daily Topic # 6 星期几(模拟)
  • 原文地址:https://www.cnblogs.com/Free-Thinker/p/5969624.html
Copyright © 2011-2022 走看看