iOS-本地通知

作者: Rick_Liu | 来源:发表于2016-05-26 11:34 被阅读257次

在iOS中,本地通知非常适用于基于时间的行为.例如,有一个需求是让用户专注做某件事情一段时间. 时间到了后给个通知告知.实现这样一个功能可以使用本地通知

实现如下效果:

Simulator Screen Shot 2016年5月26日 上午11.21.55.png Simulator Screen Shot 2016年5月26日 上午11.17.48.png

本地通知实现步骤:
1.创建本地通知对象 ( UILocalNotification )
2.设置通知的属性
3.让应用程序调用通知,使用UIApplication对象调用scheduleLocalNotification:方法
4.在iOS8.0之后,在调度通知之前需要使用 UIApplication的对象方法registerUserNotificationSettings:来请求用户授权

代码实现
定义本地通知对象属性

/** 创建本地通知对象*/
@property (nonatomic,strong) UILocalNotification *lNotification;

懒加载

//懒加载
- (UILocalNotification *)lNotification{
    
    if (!_lNotification) {
        
        _lNotification = [[UILocalNotification alloc] init];
    }
    return _lNotification;
}

设置通知的属性

 /** 设置通知的内容*/
    self.lNotification.alertBody = @"时间到了";
    
    /** 设置通知触发开始的时间*/
    self.lNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:3];
    /**  设置时区*/
    self.lNotification.timeZone = [NSTimeZone defaultTimeZone];
    
    /** 设置应用图标右上角的数字*/
    self.lNotification.applicationIconBadgeNumber = 1;
    
    /** 设置通知的音效(只有真机有效)*/
    self.lNotification.soundName = UILocalNotificationDefaultSoundName;

让应用程序调用通知

/** 让应用程序调用通知*/
    [[UIApplication sharedApplication] scheduleLocalNotification:self.lNotification];

更新显示徽章数方法

 /** 更新显示徽章数*/
    [UIApplication sharedApplication].applicationIconBadgeNumber = 0;

在iOS8.0之后,在调度通知之前需要使用 UIApplication的对象方法registerUserNotificationSettings:来请求用户授权,该方法需要在**application: didFinishLaunchingWithOptions: **中实现

/**
     *  在iOS8.0之后,在调度通知之前需要使用 UIApplication的对象方法registerUserNotificationSettings:来请求用户授权
     
        UIUserNotificationType:
     
        UIUserNotificationTypeBadge       接收到通知可更改程序的应用图标
        UIUserNotificationTypeSound       接收到通知可播放声音
        UIUserNotificationTypeAlert       接收到通知提示内容
     */
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {
        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge |UIUserNotificationTypeSound | UIUserNotificationTypeAlert categories:nil];
        [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
    }

到此,可以实现本地通知功能.

相关文章

  • iOS-本地通知

    在iOS中,本地通知非常适用于基于时间的行为.例如,有一个需求是让用户专注做某件事情一段时间. 时间到了后给个通知...

  • iOS-播放本地通知

    我的网址www.bourbonz.cn 今天我们来尝试下本地通知,内容很简单,不过首先声明一下,本地通知不同于远程...

  • IOS-本地存储-NSUserDefaults

    IOS-本地存储-NSUserDefaults 原文地址:张哲的博客 一 NSUserDefaults简介 NSU...

  • 03.iOS本地推送通知

    @(〓〓 iOS-实用技术)[远程/本地推送] 作者: Liwx 邮箱: 1032282633@qq.com 目...

  • 04.iOS远程推送通知 APNs远程推送,极光推送

    @(〓〓 iOS-实用技术)[远程/本地推送] 作者: Liwx 邮箱: 1032282633@qq.com 目...

  • iOS 通知的使用

    源地址 本地通知属于UIKit框架、推送通知属于Foundation框架。 本地通知 本地通知是由本地应用触发的,...

  • iOS-通知

    观察者模式:一种常用的设计模式,一个对象 A想了解另外一个对象B的状态是否发生了改变,在对象B上注册一个观察者,当...

  • iOS10 通知框架总结

    通知分为本地通知和远端通知,这里着重介绍本地通知。本地通知有三个步骤 Appdelegate中申请通知权限 App...

  • 通知--本地通知

    本地通知是由本地应用触发的,它是基于时间行为的一种通知形式,例如闹钟定时、待办事项提醒,又或者一个应用在一段时候后...

  • iOS 本地推送通知

    本地推送通知 对本地通知的数量限制,iOS最多允许最近本地通知数量是64个,超过限制的本地通知将被iOS忽略。 a...

网友评论

    本文标题:iOS-本地通知

    本文链接:https://www.haomeiwen.com/subject/qlfxdttx.html