SWIFT推送之本地推送(UILocalNotification)
本地推送通知是通过实例化UILocalNotification实现的。要实现本地化推送可以在AppDelegate.swift中添加代码实现,本事例是一个当App进入后台时推送一条消息给用户。
1.首先在didFinishLaunchingWithOptions方法内添加代码,IOS8推送消息首先要获得用户的同意,在初次安装App时会提示用户是否允许程序推送消息,此方法是App第一次运行的时候被执行一次,每次从后台激活时不执行该方法.
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
if (UIDevice.currentDevice().systemVersion as NSString).floatValue >= 8 {
//APService.registerForRemoteNotificationTypes(
//UIUserNotificationType.Badge.rawValue |
//UIUserNotificationType.Sound.rawValue |
//UIUserNotificationType.Alert.rawValue,
//categories: nil)
application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes:
UIUserNotificationType.Badge |
UIUserNotificationType.Sound |
UIUserNotificationType.Alert, categories: nil))
}
//APService.setupWithOption(launchOptions)
return true
}
2.有几个方法要说一下,
1.func applicationWillResignActive(application: UIApplication){} 当App既将进入后台、锁屏、有电话进来时会触发此事件
2.func applicationDidEnterBackground(application: UIApplication) {} 当App进入后台时触发此事件
3.func applicationWillEnterForeground(application: UIApplication) {} 当App从后台即将回到前台时触发此事件
4.func applicationDidBecomeActive(application: UIApplication) {}当App变成活动状态时触发此事件
5.func applicationWillTerminate(application: UIApplication) {} 当App退出时触发此方法,一般用于保存某些特定的数据
此时在applicationDidEnterBackground方法内写入以下代码:
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()
var notification = UILocalNotification()
//notification.fireDate = NSDate().dateByAddingTimeInterval(1)
//setting timeZone as localTimeZone
notification.timeZone = NSTimeZone.localTimeZone()
notification.repeatInterval = NSCalendarUnit.CalendarUnitDay
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)
}
此时将按Home键将App切换到后台时会有一条推送消息,App角标变为了“1”

3.当用户点击消息时会触发didReceiveLocalNotification事件,在这个事件内写些代码:
func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) {
UIApplication.sharedApplication().cancelAllLocalNotifications()
let userInfo = notification.userInfo!
let title = userInfo["key"] as! String
var alert = UIAlertView()
alert.title = title
alert.message = notification.alertBody
alert.addButtonWithTitle(notification.alertAction!)
alert.cancelButtonIndex = 0
alert.show()
//APService.showLocalNotificationAtFront(notification, identifierKey: nil)
}

4.当程序处于活动状态的时候清除ICON的角标
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.
//setting the desk top application icon's badge as zero
//application.applicationIconBadgeNumber = 0
application.cancelAllLocalNotifications()
application.applicationIconBadgeNumber = 0
}
SWIFT推送之本地推送(UILocalNotification)的更多相关文章
- IOS之推送通知(本地推送和远程推送)
推送通知和NSNotification是有区别的: NSNotification:是看不到的 推送通知:是可以看到的 IOS中提供了两种推送通知 本地推送通知:(Local Notification) ...
- Swift - 推送之本地推送(UILocalNotification)添加Button的点击事件
上一篇讲到的本地推送是普通的消息推送,本篇要讲一下带按钮动作的推送消息 import UIKit @UIApplicationMain class AppDelegate: UIResponder, ...
- Swift - 推送之本地推送(UILocalNotification)
// 本地推送通知是通过实例化UILocalNotification实现的.要实现本地化推送可以在AppDelegate.swift中添加代码实现,本事例是一个当App进入后台时推送一条消息给用户. ...
- 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 图片造成字符过多,拒绝显示
随机推荐
- yunw
- Z-score(Z值)的意义--转载
http://blog.sina.com.cn/s/blog_72208a6a0101cdt1.html http://www.docin.com/p-350677620.html http://we ...
- c 宏的定义
#include <stdio.h> #include <conio.h> #define VAL 40 #ifdef VAL #undef VAL #endif #defin ...
- ubuntu16.04上安装tomcat7
sudo apt-get update sudo apt-get install tomcat7 启动:sudo service tomcat7 start 访问http://127.0.0.1:80 ...
- NPOI操作
1.操作Excel 准备生成的公共方法(将数据源DataTable转换成MemoryStream) /// <summary> /// 生成Excel /// </summary&g ...
- JavaScript权威指南--多媒体和图形编程
知识要点 21.1节介绍如何用传统的JavaScript技术实现诸如图片翻转(鼠标指针移动到一张静态图片上切换成另外一张图片)这样的视觉效果. 21.2节介绍HTML5的<audio>和& ...
- Linux crontab定时执行任务 命令格式与详细例子(转)
基本格式 : * * * * * command 分 时 日 月 周 命令 第1列表示分钟1-59 每分钟用*或者 */1表示 第2列表示小时1-23(0表示0点) 第3列表示日期1-31 第4列表示 ...
- hdu6398 计算几何
不算严格的计算几何,就是各种分类 精度调好就能过,考虑三条边斜着放的所有情况即可 #include<bits/stdc++.h> #define LL long long #define ...
- spring PROPAGATION
PROPAGATION_REQUIRED Support a current transaction; create a new one if none exists. 支持一个当前事务;如果不存在 ...
- exec可以用来执行语句的
set @sql='select * from '+@table print @sql exec(@sql)