UILocalNotification本地通知

// 执行通知一定要退出应用或挂起应用(进入后台)才能收到通知。

1、在iOS8及其以后版本中使用本地消息需要先获得用户的许可,否则无法成功注册本地消息。因此,我们将询问用户许可的代码片段添加到了app启动后的入口方法中(AppDelegate中的didFinishLaunchingWithOptions

   func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        var version=UIDevice.currentDevice().systemVersion
        if (version as NSString).floatValue>=8.0{
            application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound, categories: nil))
        }

        self.window=UIWindow(frame: UIScreen.mainScreen().bounds)
        self.window!.backgroundColor=UIColor.whiteColor()
        self.window!.makeKeyAndVisible()
        self.window!.rootViewController=TB_Home()
        return true
    }

2、发送通知

    func sendLocalNotification(){

        //创建本地通知
        var notification=UILocalNotification()

        //通知触发时间(10秒后触发)
        notification.fireDate=NSDate(timeIntervalSinceNow: 10)

        //通知时区(使用本地时区)
        notification.timeZone=NSTimeZone.defaultTimeZone()

        //通知提示标题
        notification.alertTitle="messageTitle"

        //通知提示内容
        notification.alertBody="messageBody"

        //通知提示音(使用默认的通知提示音)
        notification.soundName=UILocalNotificationDefaultSoundName

        //应用程序右上角显示的数字+1
        notification.applicationIconBadgeNumber = UIApplication.sharedApplication().applicationIconBadgeNumber+1

        //启动这个通知
        UIApplication.sharedApplication().scheduleLocalNotification(notification)
    }

3、进入前台,清除右上角图标及当前应用所有通知

    func applicationWillEnterForeground(application: UIApplication) {
      application.applicationIconBadgeNumber=0  //程序右上角图标设置0

      application.cancelAllLocalNotifications()  //清除当前应用所有通知
    }
时间: 2024-08-03 13:52:46

UILocalNotification本地通知的相关文章

iOS开发中UILocalNotification本地通知实现简单的提醒功能

这段时间项目要求做一个类似的闹钟提醒功能,对通知不太熟悉的我,决定先用到xcode自带的本地通知试试,最终成功的实现了功能,特整理分享下. 它的表现特点: app关闭的时候也能接收和显示通知. app处于后台的时候能接收通知也能显示. app处于前台的时候能接收,但不能显示,但是会走应用程序delegate中的方法 具体的创建方法: ->创建一个本地通知对象UILocalNotification ->设置fireDate,AlertBody,AlertAction,soundName,appl

UILocalNotification本地通知的使用方法

本文所写方法主要应用UILocalNotification达到本地推送通知栏信息 取消了其他教程里过期的UIAlertView方法 使用UILocalNotification主要分为创建 调用 取消 三个步骤 同时注意 如果调用[NSDate dateWithTimeIntervalSince1970:alertTime]这个方法 这个时间不是从显示1970年1月1日开始计算 而是1970年1月1日8点开始计算 具体详见格林威治时间相关信息 1.创建UILocalNotification 分别在

IOS UILocalNotification 本地通知

iOS8之后,要实现badge.alert和sound等都需要用户同意才能,因为这些都算做Notification“通知”, 为了防止有些应用动不动给用户发送“通知”骚扰用户, 通知,需要我们自己注册,注册代码为 只是部分通知,仅作参考

【设定本地通知为周一到周五提醒, 周末不提醒解决办法】

iOS开发中的信息提示推送方式,一类是远程服务器推送(APNS)与UILocalNotification本地通知的,我们知道UILocalNotification的通知重复提示的单位是以是秒.分.时.天.周.月等. 如图: 那么问题来了, 要实现标题所说的该怎么办呢? 哈哈... 小编第一想到是的先判断系统时间为星期几, 然后分别设置5个以周为单位的通知, 但考虑到很麻烦, 小编本人最怕麻烦了? 于是开始向他(她)人请教. 这是位美女哦~ 这里不介绍了, 感谢~~ 说到这估计有人要砍人了, 说了

Ios开发中UILocalNotification实现本地通知实现提醒功能

这两天在做一个日程提醒功能,用到了本地通知的功能,记录相关知识如下: 1.本地通知的定义和使用: 本地通知是UILocalNotification的实例,主要有三类属性: scheduled time,时间周期,用来指定iOS系统发送通知的日期和时间: notification type,通知类型,包括警告信息.动作按钮的标题.应用图标上的badge(数字标记)和播放的声音: 自定义数据,本地通知可以包含一个dictionary类型的本地数据. 对本地通知的数量限制,iOS最多允许最近本地通知数

IOS 本地通知UILocalNotification

//发送通知    UILocalNotification *notification=[[UILocalNotification alloc] init];       if (notification!=nil) {         NSDate *now=[NSDate new];         notification.fireDate=[now dateByAddingTimeInterval:10];//10秒后通知        notification.repeatInterv

ios推送:本地通知UILocalNotification

  转载自:http://www.2cto.com/kf/201403/285612.html 在去年做过一个小App,其中使用的关键功能就是向用户发送本地通知,可惜当时没有写博客的习惯,所以没有将对应的知识记录下来.最近又遇到了该功能的使用,这一次果断写个博客做下有关UILocalNotification的笔记. 首先是添加一个本地通知到系统中,代码如下: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 2

iOS本地通知UILocalNotification

1.本地通知的定义和使用: 本地通知是UILocalNotification的实例,主要有三类属性: scheduled time,时间周期,用来指定iOS系统发送通知的日期和时间: notification type,通知类型,包括警告信息.动作按钮的标题.应用图标上的badge(数字标记)和播放的声音: 自定义数据,本地通知可以包含一个dictionary类型的本地数据. 对本地通知的数量限制,iOS最多允许最近本地通知数量是64个,超过限制的本地通知将被iOS忽略.  代码如下 复制代码

iOS本地通知:UILocalNotification

最近在做一个电商的APP,话说今年电商很火啊. 用到了本地通知,特此整理一下 添加一个本地通知到系统中,代码如下: // 初始化本地通知对象 UILocalNotification *notification = [[UILocalNotification alloc] init]; if (notification) { // 设置通知的提醒时间 NSDate *currentDate = [NSDate date]; notification.timeZone = [NSTimeZone d