本地推送通知是通过实例化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. poj 3468 A Simple Problem with Integers 线段树加延迟标记

    A Simple Problem with Integers   Description You have N integers, A1, A2, ... , AN. You need to deal ...

  2. shell 关系运算符

    关系运算符 关系运算符只支持数字,不支持字符串,除非字符串的值是数字. 下表列出了常用的关系运算符,假定变量 a 为 10,变量 b 为 20: 运算符 说明 举例 -eq 检测两个数是否相等,相等返 ...

  3. Ubuntu16.04 安装 Django

    pip2 install django==1.11 或者手动安装: 链接:https://pan.baidu.com/s/1uQJD-pON7gELoCC2TwYnEw 提取码:flgg cd Dja ...

  4. [ios]sqlite轻量级数据库学习连接

    SQLLite (一)基本介绍 http://blog.csdn.net/lyrebing/article/details/8224431 SQLLite (二) :sqlite3_open, sql ...

  5. 《F4+2团队项目需求改进与系统设计》

    任务一 a.分析<动态的太阳系模型项目需求规格说明书>初稿的不足. 任务概述描述的有些不具体,功能的规定不详细,在此次作业进行了修改. b.参考<构建之法>8.5节功能的定位和 ...

  6. 《剑指offer》第三_一题(找出数组中重复的数字,可改变数组)

    // 面试题3(一):找出数组中重复的数字 // 题目:在一个长度为n的数组里的所有数字都在0到n-1的范围内.数组中某些数字是重复的,但不知道有几个数字重复了, // 也不知道每个数字重复了几次.请 ...

  7. Python - configParser模块学习

    configParser 模块用于操作配置文件 注:Parser汉译为“解析”之意. 配置文件的格式与windows ini文件类似,可以包含一个或多个节(section),每个节可以有多个参数(键= ...

  8. SVN同步版本库与网站目录

    如何创建SVN版本库及同步文件到WEB目录 来源:空谷 一 安装与配置SVN 1.安装subversion centos: yum install subversion ubuntu: apt-get ...

  9. 关于pthread_cond_wait()使用的理解

    pthread_cond_wait()是linux多线程同步实现的一种方法,表示等待某一个线程共享变量满足了某种情况时 线程才能继续执行 pthread_cond_wait()之后的代码,如下面的示例 ...

  10. 牛客网暑期ACM多校训练营(第一场)I Substring

    题意:给你一个只有abc的字符串,求不相同的子串,(不同构算不同,例如aba和bab算同构) 题解:很显然,如果不考虑同构的问题,我们直接上sa/sam即可,但是这里不行,我们考虑到只有abc三种字符 ...