iOS本地通知

本地通知由本应用负责调用,只能从当前设备上得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)]
                }
            }
        }


    }


    }
}

效果图:

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值