您的位置:首页 > 移动开发 > IOS开发

iOS本地通知

2015-11-06 18:35 555 查看
本地通知由本应用负责调用,只能从当前设备上得iOS发出。

本地通知适用于基于时间的程序,包括简单的日历程序或者to-do列表类型的应用程序。

本地通知是一个
UILocalNotification
,它有如下属性:

var fireDate: NSDate?
:指定通知在什么时间出发

var repeatInterval: NSCalendarUnit
:设置本地通知重复发送的时间间隔

var alertBody: String?
:设置本地通知的消息体

var hasAction: Bool
:设置是否显示Action

var alertAction: String?
:设置当设备处于锁屏状态时,显示通知的警告框下方的title

var alertLaunchImage: String?
:当用户通过该通知启动对应的应用时,该属性设置为加载图片

var applicationIconBadgeNumber: Int
:设置显示在应用程序上红色徽标中的数字

var soundName: String?
:设置通知的声音

var userInfo: [NSObject : AnyObject]?
:设置该通知携带的附加信息

创建了
UILocalNotification
对象后,接下来可以通过UIApplication的如下方法发送通知:

func presentLocalNotificationNow(notification: UILocalNotification)
:该方法指定调度通知。通知将会于fireDate指定的事件触发,而且会按repeatInterval指定的时间间隔重复触发

func presentLocalNotificationNow(notification: UILocalNotification)
:该方法指定立即发送通知。该方法会忽略
UILocalNotification
的fireDate属性。

每个应用程序最多只能发送64个本地通知。

如果系统发出通知时,应用程序处于前台运行,系统将会触发应用程序委托类的方法:

optional public func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification)


应用程序需要取消本地通知时,可调用UIApplication的
func cancelLocalNotification(notification: UILocalNotification)
方法取消指定通知,或调用
func cancelAllLocalNotifications()
方法取消所有通知。

注意:

首先在
didFinishLaunchingWithOptions
方法内添加代码,IOS8推送消息首先要获得用户的同意,在初次安装App时会提示用户是否允许程序推送消息,此方法是App第一次运行的时候被执行一次,每次从后台激活时不执行该方法.

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

if (UIDevice.currentDevice().systemVersion as NSString).floatValue >= 8 {
application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes:  [UIUserNotificationType.Sound,UIUserNotificationType.Badge,UIUserNotificationType.Alert], categories: nil))

}

return true
}


全部代码如下:

AppDelegate.swift
代码

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

if (UIDevice.currentDevice().systemVersion as NSString).floatValue >= 8 {

application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes:  [UIUserNotificationType.Sound,UIUserNotificationType.Badge,UIUserNotificationType.Alert], categories: nil))

}

return true
}

func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) {
application.applicationIconBadgeNumber=0
print("ok")
}

func applicationWillResignActive(application: UIApplication) {
}

func applicationDidEnterBackground(application: UIApplication) {
}

func applicationWillEnterForeground(application: UIApplication) {
}

func applicationDidBecomeActive(application: UIApplication) {
}

func applicationWillTerminate(application: UIApplication) {
}

}


ViewController.swift
代码

import UIKit

class ViewController: UIViewController {

var app:UIApplication=UIApplication.sharedApplication()

override func viewDidLoad() {
super.viewDidLoad()

let sw:UISwitch=UISwitch(frame: CGRectMake(100,100,100,100))
self.view.addSubview(sw)

sw.addTarget(self, action: "valueChanged:", forControlEvents: .ValueChanged)

}

func valueChanged(sender:AnyObject){

print("ok")

let sw=sender as! UISwitch

if sw.on {

let notificaiton=UILocalNotification()
notificaiton.fireDate=NSDate(timeIntervalSinceNow: 10)
notificaiton.timeZone=NSTimeZone.defaultTimeZone()
notificaiton.alertAction="打开"
notificaiton.hasAction=true
notificaiton.alertLaunchImage="icon_os7"
notificaiton.alertBody="轮到你去打扫卫生了"
notificaiton.applicationIconBadgeNumber=3
notificaiton.userInfo=["ricky":"key"]
app.scheduleLocalNotification(notificaiton)
print("notification")
}
else{

let notifications:[UILocalNotification]=app.scheduledLocalNotifications!
for notification in notifications{
let dictOptional:[NSObject:AnyObject]?=notification.userInfo

if let dict=dictOptional {

let value=dict["key"] as! String

print(value)

if value=="ricky"{
[app .cancelLocalNotification(notification)]
}
}
}

}

}
}


效果图:



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: