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 ...
随机推荐
- 用Netty开发中间件:高并发性能优化(转)
用Netty开发中间件:高并发性能优化 最近在写一个后台中间件的原型,主要是做消息的分发和透传.因为要用Java实现,所以网络通信框架的第一选择当然就是Netty了,使用的是Netty 4版本.Net ...
- zend Studio10.6.2 下载
http://www.th7.cn/Program/php/201409/286788.shtml
- 002servlet生命周期以及有关servlet的各种知识
4 Sevlet的生命周期(重点) 有关servlet的类有Servlet,HttpServlet以及GenericServlet. 其实我们要写一个Servlet只要写一个类去实现Servet就可以 ...
- 编译内核出现"mkimage" command not found - U-Boot images will not be built
参考链接: http://spyker729.blogspot.com/2010/07/mkimage-command-not-found-u-boot-images.html 制作uImage的工具 ...
- 使用ANT编译项目报错 com.sun.image.codec.jpeg does not exist 解决方法
项目开发中在对图片进行裁切处理的时候,有时候是会使用到 com.sun 包下的类时. 假设项目使用ant编译,会出现错误 com.sun.image.codec.jpeg does not exist ...
- C# 创建XML文件
private void CreateXMLFile(string pathAndFileName) { XmlDocument doc = new XmlDocument(); XmlElement ...
- CImage类提供了GetBits()函数原理及实现
CImage类提供了GetBits()函数来读取数据区,GetBits()函数返回的是图片最后一行第一个像素的地址,网上有人说返回指针的起始位置是不同的,有些图片返回的是左上角像素的地址,有些是左下角 ...
- Android之ListView中的分割线
ListView中每个Item项之间都有分割线,设置android:footerDividersEnabled表示是否显示分割线,此属性默认为true. 1.不显示分割线只要在ListView控件中添 ...
- 此类目的是防治序列化Json字符串时的循环引用问题-------最好解决方案
http://james.newtonking.com/json/help/index.html using Newtonsoft.Json;using System;using System.Col ...
- Angular2+学习第3篇 基本知识-组件
一.插值表达式 基本用法与ng1一样. 可以使用 Angular 内置的 json 管道,来显示对象信息,管道用来格式化数据 import { Component } from '@angular/c ...