Swift创建Notification通知

  1. 创建一个SingleView Application
  2. 打开AppDelegate.swift,在方法

application(application:UIApplication,didFinishLaunchingWithOptions launchOptions: NSDictionary?)

中输入代码:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {

//设置Notification 的类型

let types:UIUserNotificationType = UIUserNotificationType.Alert | UIUserNotificationType.Badge

//设置Notification的设置项,其中categories参数用来设置Notification的类别

let mySettings: UIUserNotificationSettings = UIUserNotificationSettings(forType: types, categories: nil);

//注册UserNotification

UIApplication.sharedApplication().registerUserNotifiationSettings(mySettings)

return true

}

  1. 配置不同的Actions,在方法

application(application:UIApplication,didFinishLaunchingWithOptions launchOptions: NSDictionary?)内的代码开始部分输入以下代码

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {

//define Actions

var firstAction: UIMutableUserNotificationAction = UIMutableUserNotificationAction();

// The unique identifier for this action

fistAction.identifier = “FIRST_ACTION”;

// The localized title to display for this action

firstAction.title = “First Action”;

//define action’s activationMode, // How the application should be activated in response to the action

firstAction.activationMode = UIUserNotificationActivationMode.Background// 当点击的时候不启动程序,在后台处理

//define action’s destructive // Whether this action should be indicated as destructive when displayed.

firstAction.destructive = true

//define authentication // Whether this action is secure and should require unlocking before being performed. If the activation mode is UIUserNotificationActivationModeForeground, then the action is considered secure and this property is ignored.

firstAction.authenticationRequired = false//不需要用户解锁手机即可以处理该Notification

var secondAction:UIMutableUserNotificationAction = UIMutableUserNotificationAction()

secondAction.identifier = "SECOND_ACTION";

secondAction.title = "Second Action";

secondAction.activationMode = UIUserNotificationActivationMode.Foreground

secondAction.destructive = false

secondAction.authenticationRequired = false

var thirdAction:UIMutableUserNotificationAction = UIMutableUserNotificationAction()

thirdAction.identifier = "THIRD_ACTION";

thirdAction.title = "Third Action";

thirdAction.activationMode = UIUserNotificationActivationMode.Background

thirdAction.destructive = false

thirdAction.authenticationRequired = false

//Category

var firstCategory: UIMutableUserNotificationCategory = UIMutableUserNotificationCategory()

firstCategory.identifier = "FIRST_CATEGORY";

let defaultActions:NSArray = [firstAction, secondAction, thirdAction];

let minimalActions:NSArray = [firstAction, secondAction];

// Sets the UIUserNotificationActions in the order to be displayed for the specified context

firstCategory.setActions(defaultActions, forContext: UIUserNotificationActionContext.Default);// // the default context of a notification action

firstCategory.setActions(minimalActions, forContext: UIUserNotificationActionContext.Minimal);//Minimal // the context of a notification action when space is limited

let categories: NSSet = NSSet(objects: firstCategory)

//设置Notification 的类型

let types:UIUserNotificationType = UIUserNotificationType.Alert | UIUserNotificationType.Badge

//设置Notification的设置项,其中categories参数用来设置Notification的类别

let mySettings: UIUserNotificationSettings = UIUserNotificationSettings(forType: types, categories: nil);

//注册UserNotification

UIApplication.sharedApplication().registerUserNotifiationSettings(mySettings)

return true

}

4. 打开ViewController.swift文件,在ViewDidLoad方法中输入以下代码

override func viewDidLoad() {

super.viewDidLoad()

//define notification center

NSNotificationCenter.defaultCenter().addObserver(self, selector: “TestShape:”, name: “actionOne”, object: nil)

NSNotificationCenter.defaultCenter().addObserver(self, selector:”TestMessage:”, name: “actionTwo”, object: nil)

//定义一个触发Notification的程序
var dateComp: NSDateComponents  =
NSDateComponents()

dateComp.year = 2014

dateComp.month = 09

dateComp.day = 09

dateComp.hour = 11

dateComp.minute = 11

dateComp.timeZone = NSTimeZone.systemTimeZone()

var calendar: NSCalendar = NSCalendar(calendarIdentifier:
NSGregorianCalendar)

var date: NSDate = calendar.dateFromComponents(dateComp)

//define Location notification

var notification: UILocalNotification =
UILocalNotification()

notification.category = “FIRST_CATEGORY”;

notification.alertBody = “This is a notification”

notification.fireDate = date

//fire notification

UIApplication.shareApplication().scheduleLocalNotification(notification)

}

func TestShape(notification: NSNotification) {

UIView *view = UIView(frame:
CGRectMake(100,100,100,100));

view.backgroundColor = UIColor.blackColor()

Self.view.addSubview(view)

}

func TestMessage(notification: NSNotification) {

var message:
UIAlertController = UIAlertController(title: “Notification Message”, message: “Hello,
this is an alert message”, preferredStyle: UIAlertControllerStyle.Alert)

message.addAction(UIAlertAction(title: “OK”, style:
UIAlertActionStyle.Default, handle: nil))

self.presentViewController(message, animated: true,
completion: nil)

}

5. 回到AppDelegate.swift,并添加以下方法

func
application(application: UIApplication!, handleActionWithIdentifier identifier: String!,
forLocalNotification notification: UILocalNotification!, completionHandler: (() -> Void)!) {

if identifier ==
“FIRST_ACTION” {

NSNotificationCenter.defaultCenter().postNotificationName(“actionOne”,
object: nil)

}

else if identifier == “SECOND_ACTION” {

NSNotificationCenter.defaultCenter().postNotificationName(“actionTwo”,
object: nil)

}

completionHandler()

}

iPhone开发 Swift - NSNotification 通知的更多相关文章

  1. iphone开发 IOS 组织架构图

    转载自 :http://blog.csdn.net/mashi321323/article/details/18267719   登录|注册     mashi321323的专栏       目录视图 ...

  2. iPhone开发视频教程 Objective-C部分 (51课时)

    第一.二章  OC基础语法 iPhone开发教程 第一章 OC基础语法  iPhone开发概述-必看(1.1)http://www.apkbus.com/android-102215-1-1.html ...

  3. iPhone开发常问的十个问题

    iPhone开发常问的十个问题 前言 今天去stackoverflow.com上看了一下iPhone标签下排名最高的10个问题,将它们整理出来,希望这些常见问题能帮到一些iPhone开发的初学者.本来 ...

  4. iPad开发(相对于iPhone开发时专有的API)

    iPad开发 一.iPad开发简介 1.什么是iPad 一款苹果公司于2010年发布的平板电脑 定价介于苹果的智能手机iPhone和笔记本电脑产品之间 跟iPhone一样,搭载的是iOS操作系统 2. ...

  5. (转载)iPhone开发视频教程 Objective-C部分 (51课时)

      感谢好人的无私贡献!来源:http://www.cnblogs.com/aimeng/p/3370012.html   第一.二章  OC基础语法 iPhone开发教程 第一章 OC基础语法  i ...

  6. iphone开发技术要学习的内容

    一.iOS基础 1 开发环境搭建以及IOS组件.框架的概要介绍. 2 mac操作系统与iOS操作系统 3 xcode IDE开发环境的初始 二.C语言基础 1数据类型.表达式与控制流程语句 2数组.函 ...

  7. iPhone开发视频教程 Objective-C部分

    第一.二章  OC基础语法 iPhone开发教程 第一章 OC基础语法  iPhone开发概述-必看 (1.1) http://www.apkbus.com/android-102215-1-1.ht ...

  8. iPhone开发与cocos2d 经验谈

    转CSDN jilongliang : 首先,对于一个完全没有mac开发经验,甚至从没摸过苹果系统的开发人员来说,首先就是要熟悉apple的那一套开发框架(含开发环境IDE.开发框架uikit,还有开 ...

  9. iOS开发UI篇—iPad和iPhone开发的比较

    一.iPad简介 1.什么是iPad 一款苹果公司于2010年发布的平板电脑 定位介于苹果的智能手机iPhone和笔记本电脑产品之间 跟iPhone一样,搭载的是iOS操作系统 2.iPad的市场情况 ...

随机推荐

  1. 国内外从事CV相关的企业[转]

    提示:本文为笔者原创,转载请注明出处:blog.csdn.net/carson2005 经常碰到朋友问我国内从事计算机视觉(CV)领域的公司的发展情况,产品情况,甚至找工作等问题,这里,我给出自己收集 ...

  2. CXF 与Spring整合配置

    <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java ...

  3. center os 6.5 vsftpd 登陆出现 530 错误拒绝 解决方法

    别管那么多 把 /etc/vsftpd/ftpusers  里面的用户名删掉就好了.

  4. 基于FlashPaper的文档播放器

    本文主要讨论.描述了使用Adobe公司的Flex与FlashPaper产品完成对发布到网上的文档资料进行只读控制,也就是说只允许浏览操作.对下载.打印进行控制. FlashPaper FlashPap ...

  5. PCA和白化练习之处理图像

    第一步:下载pca_exercise.zip,里面包含有图像数据144*10000,每一列代表一幅12*12的图像块,首先随见展示200幅: 第二步:0均值处理,确保数据均值为0或者接近0 第三步:执 ...

  6. DebugView 调试工具

    软件标签: DebugView调试工具 用debugview,打开debugview,运行你的debug版本程序,可以定位到源文件的某一行.在vc源码中需要输出的地方用 OutputDebugStri ...

  7. 微软ASP.NET网站部署指南(4):配置项目属性

    1.  综述 有些部署设置能够在项目属性里设置的,而且保持到项目文件中(.csproj或.vbproj). 大多数情况下.你都能够在Visual Studio 选择项目属性Project Proper ...

  8. C#MongoDB 分页查询的方法及性能

    传统的SQL分页 传统的sql分页,所有的方案几乎是绕不开row_number的,对于需要各种排序,复杂查询的场景,row_number就是杀手锏.另外,针对现在的web很流行的poll/push加载 ...

  9. 基于jQuery向下弹出遮罩图片相册

    今天给大家分享一款基于jQuery向下弹出遮罩图片相册.单击相册图片时,一个遮罩层从上到下动画出现.然后弹出显示图片.这款插件适用浏览器:IE8.360.FireFox.Chrome.Safari.O ...

  10. C++ 设计模式2 (面向对象设计原则)

    1. 变化是复用的天敌! 面向对象设计的最大优势在于 : 抵御变化 2. 重新认识面向对象 理解隔离变化: 从宏观层面来看,面向对象的构建方式更能适应软件的变化, 能将变化所带来的影响减为最小. 各司 ...