Swift - 推送之本地推送(UILocalNotification)
// 本地推送通知是通过实例化UILocalNotification实现的。要实现本地化推送可以在AppDelegate.swift中添加代码实现,本事例是一个当App进入后台时推送一条消息给用户。
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// 首先在didFinishLaunchingWithOptions方法内添加代码,IOS8推送消息首先要获得用户的同意,在初次安装App时会提示用户是否允许程序推送消息,此方法是App第一次运行的时候被执行一次,每次从后台激活时不执行该方法.
if (UIDevice.currentDevice().systemVersion as NSString).floatValue >= 8 {
application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: [.Badge, .Sound, .Alert], categories: nil))
}
return true
}
// 当App既将进入后台、锁屏、有电话进来时会触发此事件
func applicationWillResignActive(application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
// 当App进入后台时触发此事件,此时在applicationDidEnterBackground方法内写入以下代码:
// 此时将按Home键将App切换到后台时会有一条推送消息,App角标变为了“1”
func applicationDidEnterBackground(application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
UIApplication.sharedApplication().cancelAllLocalNotifications()
let notification = UILocalNotification()
//notification.fireDate = NSDate().dateByAddingTimeInterval(1)
//setting timeZone as localTimeZone
notification.timeZone = NSTimeZone.localTimeZone()
notification.repeatInterval = NSCalendarUnit.Day
notification.alertTitle = "This is a local notification"
notification.alertBody = "Hey,It's great to see you again"
notification.alertAction = "OK"
notification.soundName = UILocalNotificationDefaultSoundName
//setting app's icon badge
notification.applicationIconBadgeNumber = 1
var userInfo:[NSObject : AnyObject] = [NSObject : AnyObject]()
userInfo["kLocalNotificationID"] = "LocalNotificationID"
userInfo["key"] = "Attention Please"
notification.userInfo = userInfo
//UIApplication.sharedApplication().scheduleLocalNotification(notification)
//UIApplication.sharedApplication().presentLocalNotificationNow(notification)
application.presentLocalNotificationNow(notification)
}
// 当用户点击消息时会触发didReceiveLocalNotification事件,在这个事件内写些代码:
func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) {
UIApplication.sharedApplication().cancelAllLocalNotifications()
let userInfo = notification.userInfo!
let title = userInfo["key"] as! String
let alert = UIAlertView()
alert.title = title
alert.message = notification.alertBody
alert.addButtonWithTitle(notification.alertAction!)
alert.cancelButtonIndex = 0
alert.show()
}
// 当App从后台即将回到前台时触发此事件
func applicationWillEnterForeground(application: UIApplication) {
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}
// 当App变成活动状态时触发此事件
func applicationDidBecomeActive(application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
// 当程序处于活动状态的时候清除ICON的角标
application.cancelAllLocalNotifications()
application.applicationIconBadgeNumber = 0
}
// 当App退出时触发此方法,一般用于保存某些特定的数据
func applicationWillTerminate(application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
Swift - 推送之本地推送(UILocalNotification)的更多相关文章
- IOS之推送通知(本地推送和远程推送)
推送通知和NSNotification是有区别的: NSNotification:是看不到的 推送通知:是可以看到的 IOS中提供了两种推送通知 本地推送通知:(Local Notification) ...
- Swift - 推送之本地推送(UILocalNotification)添加Button的点击事件
上一篇讲到的本地推送是普通的消息推送,本篇要讲一下带按钮动作的推送消息 import UIKit @UIApplicationMain class AppDelegate: UIResponder, ...
- SWIFT推送之本地推送(UILocalNotification)
本地推送通知是通过实例化UILocalNotification实现的.要实现本地化推送可以在AppDelegate.swift中添加代码实现,本事例是一个当App进入后台时推送一条消息给用户. 1.首 ...
- SWIFT推送之本地推送(UILocalNotification)之二带按钮的消息
上一篇讲到的本地推送是普通的消息推送,本篇要讲一下带按钮动作的推送消息,先上个图瞅瞅: 继上一篇的内容进行小小的改动: 在didFinishLaunchingWithOptions方法内进行以下修改 ...
- IOS中程序如何进行推送消息(本地推送,远程推送)
[1]-------------什么是推送消息? 我就以一张图解释------------ [2]-----------IOS程序中如何进行本地推送?----------- 2.1,先征求用户同意 1 ...
- ios10 UNNtificationRequest UNUserNotificationCenter的应用 推送之本地推送
iOS10 已经 "deprected" 我们的UILocalNotification 采用了全新的UNUserNotificationCenter; 1 首先,你需要引进< ...
- [iOS 高级] iOS远程推送与本地推送大致流程
本地推送: UILocalNotification *notification=[[UILocalNotification alloc] init]; if (notification!=nil) { ...
- IOS中程序如何进行推送消息(本地推送,远程推送)2(上)
未看过本地推送的,可以提前看一下本地推送. http://www.cnblogs.com/wolfhous/p/5135711.html =============================== ...
- IOS中程序如何进行推送消息(本地推送,远程推送)2(下)
内容中包含 base64string 图片造成字符过多,拒绝显示
随机推荐
- C 文件读写 容易疏忽的一个问题
今天需要解决一个问题,将影像瓦片(一堆jpg文件)分别进行读取,并将所有数据以文件流的方式存入一个.db的文件中, 同时将每个jpg数据在db文件中的位置保存下来,作为index存在.idx文件中. ...
- 原生态js,鼠标按下后,经过了那些单元格
本来是要判断那些单元格被选中,结果发现行不通,只能判断鼠标按下后,经过了那些单元格 之所以发出来,是觉得案例还有很多有意思的地方 onmouseover 的持续触发,导致了很多重复元素 由于将事件绑 ...
- MySQL中快速复制数据表方法汇总
本文将着重介绍两个MySQL命令的组合,它将以原有数据表为基础,创建相同结构和数据的新数据表. 这可以帮助你在开发过程中快速的复制表格作为测试数据,而不必冒险直接操作正在运行 的数据表. 示例如下: ...
- 关于ListView中notifyDataSetChanged()刷新数据不更新原因
使用Listview的时候: 当要动态显示更改后的数据(例如数据库改动), 很多人应该都用过notifyDataSetChanged();这个方法来刷新Listview,显示改后的数据. 这时候就要注 ...
- HDu1003(maxn sum)
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABBcAAAMDCAYAAAD5XP0yAAAgAElEQVR4nOy97a8c133n2X+H3xjIC4
- C++变量对比java变量所占内存
C++ char 1 short int 2 int 4 long int 8 float 4 double 8 long double 8 下面是计算程序: #include<math.h&g ...
- PHP--获取响应头(Response Header)方法
方法一: $baiduUrl = "http://www.baidu.com/link"; file_get_contents($baiduUrl); $responseInf ...
- Walls and Gates
You are given a m x n 2D grid initialized with these three possible values. -1 - A wall or an obstac ...
- liunux mysql MySQL表名不区分大小写的设置方法
原来Linux下的MySQL默认是区分表名大小写的,通过如下设置,可以让MySQL不区分表名大小写:1.用root登录,修改 /etc/my.cnf:2.在[mysqld]节点下,加入一行: lowe ...
- MySQL 主键范围查找问题
背景: 今天遇到一个主键范围查找的情况: id是主键,每次取10000.上面的这个查询id范围越往后面消耗的时间越久.通过id自增主键去查找数据应该不会出现这个现象的.以前都没有注意这个奇怪的现象,现 ...