本地推送通知是通过实例化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)的更多相关文章

  1. IOS之推送通知(本地推送和远程推送)

    推送通知和NSNotification是有区别的: NSNotification:是看不到的 推送通知:是可以看到的 IOS中提供了两种推送通知 本地推送通知:(Local Notification) ...

  2. Swift - 推送之本地推送(UILocalNotification)添加Button的点击事件

    上一篇讲到的本地推送是普通的消息推送,本篇要讲一下带按钮动作的推送消息 import UIKit @UIApplicationMain class AppDelegate: UIResponder, ...

  3. Swift - 推送之本地推送(UILocalNotification)

    // 本地推送通知是通过实例化UILocalNotification实现的.要实现本地化推送可以在AppDelegate.swift中添加代码实现,本事例是一个当App进入后台时推送一条消息给用户. ...

  4. SWIFT推送之本地推送(UILocalNotification)之二带按钮的消息

    上一篇讲到的本地推送是普通的消息推送,本篇要讲一下带按钮动作的推送消息,先上个图瞅瞅: 继上一篇的内容进行小小的改动: 在didFinishLaunchingWithOptions方法内进行以下修改 ...

  5. IOS中程序如何进行推送消息(本地推送,远程推送)

    [1]-------------什么是推送消息? 我就以一张图解释------------ [2]-----------IOS程序中如何进行本地推送?----------- 2.1,先征求用户同意 1 ...

  6. ios10 UNNtificationRequest UNUserNotificationCenter的应用 推送之本地推送

    iOS10 已经 "deprected" 我们的UILocalNotification 采用了全新的UNUserNotificationCenter; 1 首先,你需要引进< ...

  7. [iOS 高级] iOS远程推送与本地推送大致流程

    本地推送: UILocalNotification *notification=[[UILocalNotification alloc] init]; if (notification!=nil) { ...

  8. IOS中程序如何进行推送消息(本地推送,远程推送)2(上)

    未看过本地推送的,可以提前看一下本地推送. http://www.cnblogs.com/wolfhous/p/5135711.html =============================== ...

  9. IOS中程序如何进行推送消息(本地推送,远程推送)2(下)

    内容中包含 base64string 图片造成字符过多,拒绝显示

随机推荐

  1. cygwin install git

    Installation with Cygwin If you're comfortable with Cygwin, then use it to install git, ssh, wget an ...

  2. CAS-自旋锁

    自旋锁 自旋锁(spinlock):是指当一个线程在获取锁的时候,如果锁已经被其它线程获取,那么该线程将循环等待,然后不断的判断锁是否能够被成功获取,直到获取到锁才会退出循环.  获取锁的线程一直处于 ...

  3. arcgis for silverlight 地图放大到某个点或者几何对象

    http://blog.csdn.net/xuan444150/article/details/7727866   分类: silverlight王国 GIS王国 2012-07-09 08:50 1 ...

  4. 一篇文章,读懂Netty的高性能架构之道

    一篇文章,读懂Netty的高性能架构之道 Netty是由JBOSS提供的一个java开源框架,是一个高性能.异步事件驱动的NIO框架,它提供了对TCP.UDP和文件传输的支持,作为一个异步NIO框架, ...

  5. python 爬虫之为什么使用opener对象以及为什么要创建全局默认的opener对象

    基本的urlopen()函数不支持验证.cookie或其他HTTP高级功能.要支持这些功能,必须使用build_opener()函数来创建自己的自定义Opener对象. install_opener( ...

  6. JS-图片控制-动画管理模块

    animateManage.js ;(function(window,document,undefined){ var _aniQueue = [], //动画队列 --- ani:动画,Queue: ...

  7. 84. Largest Rectangle in Histogram *HARD* -- 柱状图求最大面积 85. Maximal Rectangle *HARD* -- 求01矩阵中的最大矩形

    1. Given n non-negative integers representing the histogram's bar height where the width of each bar ...

  8. 54. 59. Spiral Matrix

    1. Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral ...

  9. iOS UI-AlertView(警示框)和ActionSheet(选择框、操作表单)

    #import "ViewController.h" @interface ViewController ()<UIAlertViewDelegate,UIActionShe ...

  10. zabbix自动化监控基础

    zabbix安装配置文档 2 一 zabbix-server 安装配置(基础配置) 2 二 zabbix agent安装配置 5 2.1 主动模式和被动模式 6 2.2 安装配置zabbix_agen ...