UILocalNotification 的使用
@IBAction func sendNotification(sender: AnyObject) {
var userInfo = Dictionary<String,String>()
userInfo["uid"]="123456"
localNotification.alertAction = "Testing notifications on iOS8"
localNotification.alertBody = "WoWWWWWW background"
// 触发时间
localNotification.fireDate = NSDate() //立即触发
//localNotification.fireDate = NSDate().dateByAddingTimeInterval(10) //10s后触发
localNotification.applicationIconBadgeNumber = 7 //设置app 图标上的数字
localNotification.timeZone = NSTimeZone.defaultTimeZone()
//设置重复触发的间隔, 不设置的时候,只发一次. 这里的间隔只能是固定的几种日历单位,不能随意设置间隔
localNotification.repeatInterval = NSCalendarUnit.MinuteCalendarUnit
localNotification.userInfo = userInfo
UIApplication.sharedApplication().scheduleLocalNotification(localNotification) //按照 localNotification 设置的时间触发
//UIApplication.sharedApplication().presentLocalNotificationNow(localNotification) //马上触发
}
这种通知的限制是,只有当 App 的后台运行的时候或者没有运行的时候(一段时间内,应该是十分钟)才会触发. 如果要在前台运行的时候也触发,需要重写 Appdelegate 中的func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification)
基于 AppDelegate 的这种实现, 可以变线的实现在上面localNotification.repeatInterval 无法实现的自定义时间间隔触发的功能. 也就是在 AppDelegate 中接收到通知后自己再次发送通知
func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) {
var userInfoString = ""
localNotification.alertAction = notification.alertAction
if let userInfoDic = notification.userInfo as? Dictionary<String,String> {
//这里可以获取到发送通知时设置的 userInfo
userInfoString = userInfoDic["uid"]!
}
localNotification.alertBody = notification.alertBody!+"from AppDelegate:\(userInfoString)"
localNotification.fireDate = NSDate().dateByAddingTimeInterval(10)
UIApplication.sharedApplication().scheduleLocalNotification(localNotification)
}
最后, 已经注册的通知,要手动 cancel 掉.
一种简单粗暴, cancel 掉所有的:
UIApplication.sharedApplication().cancelAllLocalNotifications()
另外如果要只 cancel 指定的通知, 可以利用 userInfo 来实现.比如下面的通过自定义的一个 Uid 来实现
func cancelLocalNotificationByUid(uid: String) {
let allLocalNotifications = UIApplication.sharedApplication().scheduledLocalNotifications as! [UILocalNotification]
for currentLocalNotification in allLocalNotifications {
if let userInfo = currentLocalNotification.userInfo as? Dictionary<String,String> {
if userInfo["uid"] == uid {
UIApplication.sharedApplication().cancelLocalNotification(currentLocalNotification)
}
}
}
}
UILocalNotification 的使用的更多相关文章
- iOS调试通过UILocalNotification或RemoteNotification启动的app
相信很多同学都为调试苹果的通知烦恼过,特别是通过通知启动app这个功能,简直让人欲哭无泪!!! 然而我们都遇到的问题,苹果怎么可能没有想到,原来早就有了官方的解决办法,只是我们不知道而已... 这次又 ...
- 用UILocalNotification实现一个闹钟(Swift)
之前项目需求要实现一个闹钟,github上找了半天发现都是很旧的代码了,所以就准备自己写一个,刚好最近在学习Swift,就用Swift写了一个demo放在这里:https://github.com/P ...
- UILocalNotification本地通知的使用方法
本文所写方法主要应用UILocalNotification达到本地推送通知栏信息 取消了其他教程里过期的UIAlertView方法 使用UILocalNotification主要分为创建 调用 取消 ...
- 本地推送UILocalNotification
//本地推送---无需网络,由本地发起 UILocalNotification *localNotification = [[UILocalNotification alloc]init]; //设置 ...
- ios 把已经点击过的UILocalNotification 从系统的通知中心现实中移除
在ios7 上一个uilocalnotification在中心现实后,点击该消息,程序被唤醒了,但是该通知没有被移除.用了以下的代码后可以解决这个问题 UIApplication.sh ...
- iOS UILocalNotification 每2周,每两个月提醒
iOS 的UILocalNotification提醒提供了默认的重复频率,比如,一天,一个星期等等,但是对于非标准的频率,比如每,2周,每2个月,无法重复提醒. 我们的思路是在应用程序开始时,把即将发 ...
- iOS - iPhone开发 UILocalNotification的使用
OS下的Notification的使用 Notification 是智能手机应用编程中非常常用的一种传递信息的机制,而且可以非常好的节省资源,不用消耗资源来不停地检查信息状态(Pooling),在iO ...
- Swift - 推送之本地推送(UILocalNotification)添加Button的点击事件
上一篇讲到的本地推送是普通的消息推送,本篇要讲一下带按钮动作的推送消息 import UIKit @UIApplicationMain class AppDelegate: UIResponder, ...
- Swift - 推送之本地推送(UILocalNotification)
// 本地推送通知是通过实例化UILocalNotification实现的.要实现本地化推送可以在AppDelegate.swift中添加代码实现,本事例是一个当App进入后台时推送一条消息给用户. ...
- ios推送:本地通知UILocalNotification
Notification是智能手机应用编程中非常常用的一种传递信息的机制,而且可以非常好的节省资源,不用消耗资源来不停地检查信息状态(Pooling),在iOS下应用分为两种不同的Notificati ...
随机推荐
- 随机算法 - HNU 13348 Finding Lines
Finding Lines Problem's Link: http://acm.hnu.cn/online/?action=problem&type=show&id=13348&am ...
- 第二百五十二节,Bootstrap项目实战-首页
Bootstrap项目实战-首页 html <!DOCTYPE html> <html lang="zh-cn"> <head> <met ...
- php -- 修改字符串的编码格式
网上的都是这样用的 $content = iconv("utf-8","gb2312",$content); 这样做其实也对着了,看着确实是把utf-8转化为g ...
- php -- php缓存技术
为了尽量减少不必要的数据库查询,我对一些数据进行了缓存和静态化处理. 缓存的原理:把一些经常要用到但又很少改动的数据以数组或其它形式存储到一个独立的PHP文件中,然后在需要用到的时候包含进来. 缓存的 ...
- mysql -- 创建存储过程 往数据表中新增字段
需求: 往某数据库的某个表中新增一个字段(若该字段已存在,则不做操作:若该字段不存在,则新增) 百度了n久,没有符合要求的例子,只有参考加自己琢磨,最终终于给弄出来了,以下是几个版本的更迭 第一版: ...
- 【BZOJ】1639: [Usaco2007 Mar]Monthly Expense 月度开支(二分)
http://www.lydsy.com/JudgeOnline/problem.php?id=1639 同tyvj1359,http://www.cnblogs.com/iwtwiioi/p/394 ...
- Mac 文件读写权限问题 OSError: Operation not permitted
Mac在OS X 10.11以后加入了Rootless功能,主要是限制了root权限,阻止用户对部分路径下的目录进行更改.受到限制的有以下目录: /System /bin /sbin /usr (ex ...
- sdut 2158:Hello World!(第一届山东省省赛原题,水题,穷举)
Hello World! Time Limit: 1000MS Memory limit: 65536K 题目描述 We know that Ivan gives Saya three problem ...
- Docker(1)在CentOS上的安装与卸载
一. Docker的安装 CentOS7 上安装: 1. 卸载旧版本 $ sudo yum remove docker \ docker-client \ docker-client-latest ...
- Fragment遇到的坑
Fragment不要通过构造传参,要么就是bundle,要么就通过activity临时存一下,不然debug编译没问题release编译不过