使用UILocalNotification可以很方便的实现消息的推送功能。我们可以设置这个消息的推送时间,推送内容等。

当推送时间一到,不管用户在桌面还是其他应用中,屏幕上方会都显示出推送消息。

1,推送消息的发送

   

--- AppDelegate.swift ---

 
import UIKit
 
@UIApplicationMain
class AppDelegateUIResponderUIApplicationDelegate {
 
    var window: UIWindow?
 
    func application(application: UIApplication,
        didFinishLaunchingWithOptions
launchOptions: [
NSObjectAnyObject]?)
-> 
Bool {
        //开启通知
        let settings
UIUserNotificationSettings(forTypes:
[.
Alert,
.
Badge,
.
Sound],
            categories: nil)
        application.registerUserNotificationSettings(settings)
        return true
    }
 
    func applicationWillResignActive(application: UIApplication)
{
    }
 
    func applicationDidEnterBackground(application: UIApplication)
{
    }
 
    func applicationWillEnterForeground(application: UIApplication)
{
    }
 
    func applicationDidBecomeActive(application: UIApplication)
{
    }
 
    func applicationWillTerminate(application: UIApplication)
{
    }
}

--- ViewController.swift ---

 
import UIKit
 
class ViewControllerUIViewController {
    override func viewDidLoad()
{
        super.viewDidLoad()
         
        //发送通知消息
        scheduleNotification(12345);
        //清除所有本地推送
        //UIApplication.sharedApplication().cancelAllLocalNotifications()
    }
     
    //发送通知消息
    func scheduleNotification(itemID:Int){
        //如果已存在该通知消息,则先取消
        cancelNotification(itemID)
         
        //创建UILocalNotification来进行本地消息通知
        let localNotification
UILocalNotification()
        //推送时间(设置为30秒以后)
        localNotification.fireDate
NSDate(timeIntervalSinceNow:
30)
        //时区
        localNotification.timeZone
NSTimeZone.defaultTimeZone()
        //推送内容
        localNotification.alertBody
"来自hangge.com的本地消息"
        //声音
        localNotification.soundName
UILocalNotificationDefaultSoundName
        //额外信息
        localNotification.userInfo
= [
"ItemID":itemID]
        UIApplication.sharedApplication().scheduleLocalNotification(localNotification)
    }
     
    //取消通知消息
    func cancelNotification(itemID:Int){
        //通过itemID获取已有的消息推送,然后删除掉,以便重新判断
        let existingNotification
self.notificationForThisItem(itemID) as UILocalNotification?
        if existingNotification
!= 
nil {
            //如果existingNotification不为nil,就取消消息推送
            UIApplication.sharedApplication().cancelLocalNotification(existingNotification!)
        }
    }
     
    //通过遍历所有消息推送,通过itemid的对比,返回UIlocalNotification
    func notificationForThisItem(itemID:Int)-> UILocalNotification?
{
        let allNotifications
UIApplication.sharedApplication().scheduledLocalNotifications
        for notification in allNotifications!
{
            let info
= notification.userInfo 
as!
[
String:Int]
            let number
= info[
"ItemID"]
            if number
!= 
nil &&
number == itemID {
                return notification as UILocalNotification
            }
        }
        return nil
    }
     
    override func didReceiveMemoryWarning()
{
        super.didReceiveMemoryWarning()
    }
}

2,点击推送消息的响应

收到推送,如果点击推送内容,则会重新进入到App,这个时候会调用AppDelegate中的func
application(application: UIApplication, didReceiveLocalNotification
notification: UILocalNotification)代理方法。
在这个方法中我们可以根据推送的消息内容实现相关的功能。

 
func application(application: UIApplication,
    didReceiveLocalNotification
notification: 
UILocalNotification)
{
        //设定Badge数目
        UIApplication.sharedApplication().applicationIconBadgeNumber
= 0
             
        let info
= notification.userInfo 
as!
[
String:Int]
        let number
= info[
"ItemID"]
             
        let alertController
UIAlertController(title: "本地通知",
                message: "消息内容:\(notification.alertBody)用户数据:\(number)",
                preferredStyle: UIAlertControllerStyle.Alert)
        

 
 
          let cancel = UIAlertAction(title: "取消", style: UIAlertActionStyle.Cancel, handler: nil);

alertController.addAction(cancel);

     

        self.window?.rootViewController!.presentViewController(alertController,
                animated: true,
completion: 
nil)
}

Swift - 本地消息的推送通知(附样例)的更多相关文章

  1. 基于swoole+Redis的消息实时推送通知

    swoole+Redis将实时数据的推送 一 实现功能 设计师订单如果设计师未抢单,超时(5分钟)设计订单时时给设计师派送, 设计师公众号中收到派单信息 设计发布者收到派单成功信息 环境 centos ...

  2. 移动 UX 设计:如何设计推送通知

    这个问题你一定想过,在移动用户体验设计领域中,如何设计好一条简单的推送通知. 你注意过么,每天从不同的 App 上收到的大量的推送通知与提醒,这些通知里有多少你真的有兴趣? 每天,用户对各种没用的通知 ...

  3. 背水一战 Windows 10 (121) - 后台任务: 推送通知

    [源码下载] 背水一战 Windows 10 (121) - 后台任务: 推送通知 作者:webabcd 介绍背水一战 Windows 10 之 后台任务 推送通知 示例演示如何接收推送通知/WebA ...

  4. APNS推送通知消息负载内容和本地格式字符串

    来源:http://hi.baidu.com/tangly888/blog/item/62948520121870559358074f.html 翻译苹果文档 地址:  翻译:tangly http: ...

  5. Swift 本地推送通知UILocalNotification

    Notification是智能手机应用开发中常用的信息传递机制,它不用消耗更多资源去不停的检查信息状态,可以非常好的节省资源. 在iOS中分为两种通知:本地.远程.本地的UILocalNotifica ...

  6. IOS之推送通知(本地推送和远程推送)

    推送通知和NSNotification是有区别的: NSNotification:是看不到的 推送通知:是可以看到的 IOS中提供了两种推送通知 本地推送通知:(Local Notification) ...

  7. (七十三)iOS本地推送通知的实现

    iOS的推送通知分为本地推送和网络推送两种,如果App处于挂起状态,是可以发送本地通知的,如果已经被杀掉,则只有定时通知可以被执行,而类似于QQ的那种网络消息推送就无法实现了,因为App的网络模块在被 ...

  8. iOS 通知、本地通知和推送通知有什么区别? APNS机制。

    本地/推送通知为不同的需要而设计.本地通知对于iPhone,iPad或iPod来说是本地的.而推送通知——来自于设备外部.它们来自远程服务器——也叫做远程通知——推送给设备上的应用程序(使用APNs) ...

  9. iOS 本地推送通知

    1.什么是本地推送通知 不需要联网的情况下,应用程序经由系统发出的通知 2.本地推送的使用场景 定时提醒,如玩游戏.记账.闹钟.备忘录等 3.实现本地推送通知的步骤 创建本地推送通知的对象UILoca ...

随机推荐

  1. leetcode:Roman to Integer(罗马数字转化为罗马数字)

    Question: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the rang ...

  2. 学习Python必须要知道的常用模块

    在程序设计中,为完成某一功能所需的一段程序或子程序:或指能由编译程序.装配程序等处理的独立程序单位:或指大型软件系统的一部分.本文为你介绍了Python中的两种常用模块. os: 这个模块包含普遍的操 ...

  3. CodeIgniter 3.0+ 部署linux环境 session报错

    codeigniter Message: mkdir(): Invalid path Filename: drivers/Session_files_driver.php 看起来像权限问题,在默认情况 ...

  4. 关于dom节点绑定滑动事件导致浏览器上下滑动失效解决方案--黄丕巧

    1.移动端开发往往需要添加一下自定义的左右滑动事件,但是添加了左右滑动事件之后就要阻止浏览器大默认事件,否则dom节点的滑动事件和浏览器本身的滑动会出现冲突,导致滑动的时候会出现消失瞬间再出现的效果 ...

  5. MyEclipse 代码自动提示

    默认 myeclipse,输入.才会出现代码提示,不能根据单词前缀提示. 按以下设置就很爽了 .abcdefghijklmnopqrstuvwxyz(,

  6. XE8 hash

    c++builder  xe8 hash calc md5.sha256.sha384.sha512 file and string sha256.sha384.sha512  must call l ...

  7. c/c++,输入一个字符 2014-11-20 07:00 30人阅读 评论(0) 收藏

    getch().getche()和getchar()函数     (1) getch()和getche()函数     这两个函数都是从键盘上读入一个字符.其调用格式为:      getch(); ...

  8. JVM的内存管理机制

    在做Java开发的时候常用的JVM内存管理有两种,一种是堆内存,一种是栈内存.堆内存主要用来存储程序在运行时创建或实例化的对象与变量,例如:我们通过new MyClass()创建的类MyClass的对 ...

  9. centOS安装openoffice

    centOS安装openoffice的方法: yum install openoffice.org-writer yum install openoffice.org-calc yum install ...

  10. 结构类模式(五):外观(Facade)

    定义 为子系统中的一组接口提供一个一致的界面,定义一个高层接口,这个接口使得这一子系统更加容易使用. UML 优点 对客户屏蔽了其子系统组件,因而减少了客户处理对象的数目,并使得子系统实用起来更方便. ...