布局如下:(重点讲本地通知)

iOS开发者交流QQ群: 446310206

每日更新关注:http://weibo.com/hanjunqiang 
新浪微博

Notification是智能手机应用编程中非常常用的一种传递信息的机制,而且可以非常好的节省资源,不用消耗资源来不停地检查信息状态(Pooling),在iOS下应用分为两种不同的Notification种类,本地和远程。本地的Notification由iOS下NotificationManager统一管理,只需要将封装好的本地Notification对象加入到系统Notification管理机制队列中,系统会在指定的时间激发将本地Notification,应用只需设计好处理Notification的方法就完成了整个Notification流程了。

本地Notification所使用的对象是UILocalNotification,UILocalNotification的属性涵盖了所有处理Notification需要的内容。UILocalNotification的属性有fireDate、timeZone、repeatInterval、repeatCalendar、alertBody、
alertAction、hasAction、alertLaunchImage、applicationIconBadgeNumber、 soundName和userInfo。

每日更新关注:http://weibo.com/hanjunqiang 
新浪微博

1.首先要明白模拟器和真机的区别:模拟器不会有音频提示,另外就是没有检测允许接受通知,所以我补充一下几点:

1.添加监测通知:

 if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)]){

        [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];
    }

上代码:

#import "ViewController.h"
#import "DetailViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIButton *schedule;
@property (weak, nonatomic) IBOutlet UIButton *unSchedule;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

// 调度通知
- (IBAction)schedule:(UIButton *)sender {

    // 1.创建通知
    UILocalNotification *ln = [[UILocalNotification alloc]init];

    if (ln) {
        // 设置时区
        ln.timeZone = [NSTimeZone defaultTimeZone];
        // 通知第一次发出的时间
        ln.fireDate = [[NSDate date]dateByAddingTimeInterval:5];

        // 2.设置通知属性
        ln.soundName = @"click.wav"; // 音效文件名
        // 通知的具体内容
        ln.alertBody = @"重大新闻:小韩哥的博客又更新了,赶快进来看看吧!....";

        // 锁屏界面显示的小标题,完整标题:(“滑动来”+小标题)
        ln.alertAction = @"查看新闻吧";

        // 设置app图标数字
        ln.applicationIconBadgeNumber = 10;

        // 设置app的额外信息
        ln.userInfo = @{
                        @"icon":@"text.png",
                        @"title":@"重大新闻",
                        @"time":@"2016-02-28",
                        @"body":@"重大新闻:小韩哥的博客又更新了,赶快进来看看吧!"
                        };
        // 设置重启图片
        ln.alertLaunchImage = @"101339g76j7j9t2zgzdvkj.jpg";

        // 设置重复发出通知的时间间隔
//        ln.repeatInterval = NSCalendarUnitMinute;

        // 3.调度通知(启动任务,在规定的时间发出通知)
        [[UIApplication sharedApplication]scheduleLocalNotification:ln];
        // 直接发出通知没意义
//        [[UIApplication sharedApplication]presentLocalNotificationNow:ln];
    }

}
- (IBAction)noSchedule:(UIButton *)sender
{
//    [[UIApplication sharedApplication]cancelAllLocalNotifications];
    // 已经发出且过期的通知会从数组里自动移除
    NSArray *notes = [UIApplication sharedApplication].scheduledLocalNotifications;
    NSLog(@"%@",notes);
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(UILocalNotification *)note
{
    DetailViewController *detailVC = segue.destinationViewController;
    detailVC.userInfo = note.userInfo;
}
@end

2.通知详情页面设置基本属性:

每日更新关注:http://weibo.com/hanjunqiang 
新浪微博

.h
#import <UIKit/UIKit.h>
@interface DetailViewController : UIViewController

@property (nonatomic, strong) NSDictionary *userInfo;
@end

.m
#import "DetailViewController.h"

@interface DetailViewController ()
@property (weak, nonatomic) IBOutlet UILabel *userInfoContent;

@end

@implementation DetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];

     self.userInfoContent.text = self.userInfo[@"body"];
}

- (void)setUserInfo:(NSDictionary *)userInfo
{
    _userInfo = userInfo;
}
@end

3.didFinishLaunchingWithOptions 实时监测:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    //注册本地通知

    if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)]){

        [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];
    }

//    NSLog(@"-----didFinishLaunchingWithOptions---");
    UILabel *label = [[UILabel alloc]init];
    label.frame = CGRectMake(0, 64, 320, 100);
    label.backgroundColor = [UIColor redColor];
    label.font = [UIFont systemFontOfSize:11];
    label.numberOfLines = 0;
    label.textColor = [UIColor whiteColor];
    label.text = [launchOptions description];
    [[[self.window.rootViewController.childViewControllers firstObject] view]addSubview:label];

    UILocalNotification *note = launchOptions[UIApplicationLaunchOptionsURLKey];
    if (note) {
        label.text = @"点击本地通知启动的程序";
    }else{
        label.text = @"直接点击app图标启动的程序";
    }
    self.label = label;
    return YES;
}
/**
 * 当用户点击本地通知进入app的时候调用(app当时并没有被关闭)
 * 若app已关闭不会被调用此方法
 */
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
    self.label.text = @"点击通知再次回到前台";
    ViewController *homeVC = [self.window.rootViewController.childViewControllers firstObject];
//    [homeVC performSegueWithIdentifier:@"toHome" sender:notification];
    [homeVC performSegueWithIdentifier:@"toHome" sender:notification];

}

三种情况展示:(重要)

每日更新关注:http://weibo.com/hanjunqiang 
新浪微博

1.程序运行在后台

每日更新关注:http://weibo.com/hanjunqiang 
新浪微博

Demo下载地址Github:  https://github.com/XiaoHanGe/LocalNotification

iOS开发者交流QQ群:
446310206

iOS中 本地通知/本地通知详解 韩俊强的博客的更多相关文章

  1. iOS中 扫描二维码/生成二维码详解 韩俊强的博客

    最近大家总是问我有没有关于二维码的demo,为了满足大家的需求,特此研究了一番,希望能帮到大家! 每日更新关注:http://weibo.com/hanjunqiang  新浪微博 指示根视图: se ...

  2. iOS中 HTTP/Socket/TCP/IP通信协议详解 韩俊强的博客

    每日更新关注:http://weibo.com/hanjunqiang  新浪微博 简单介绍: // OSI(开放式系统互联), 由ISO(国际化标准组织)制定 // 1. 应用层 // 2. 表示层 ...

  3. iOS中 最新微信支付/最全的微信支付教程详解 韩俊强的博客

    每日更新关注:http://weibo.com/hanjunqiang  新浪微博! 亲们, 首先让我们来看一下微信支付的流程吧. 1. 注册微信开放平台,创建应用获取appid,appSecret, ...

  4. iOS中 蓝牙2.0详解/ios蓝牙设备详解 韩俊强的博客

    每日更新关注:http://weibo.com/hanjunqiang  新浪微博 整体布局如下:     程序结构如右图: 每日更新关注:http://weibo.com/hanjunqiang  ...

  5. iOS中 CoreGraphics快速绘图(详解) 韩俊强的博客

    每日更新关注:http://weibo.com/hanjunqiang  新浪微博 第一步:先科普一下基础知识: Core Graphics是基于C的API,可以用于一切绘图操作 Core Graph ...

  6. iOS中 语音识别功能/语音转文字教程详解 韩俊强的博客

    每日更新关注:http://weibo.com/hanjunqiang  新浪微博 原文地址:http://blog.csdn.net/qq_31810357/article/details/5111 ...

  7. iOS中 断点下载详解 韩俊强的博客

    布局如下: 基本拖拉属性: #import "ViewController.h" #import "AFNetworking.h" @interface Vie ...

  8. iOS中 项目开发易错知识点总结 韩俊强的博客

    每日更新关注:http://weibo.com/hanjunqiang  新浪微博! 点击return取消textView 的响应者 - (BOOL)textFieldShouldReturn:(UI ...

  9. iOS中 支付宝钱包具体解释/第三方支付 韩俊强的博客

    每日更新关注:http://weibo.com/hanjunqiang  新浪微博! iOS开发人员交流QQ群: 446310206 一.在app中成功完毕支付宝支付的过程 1.申请支付宝钱包.參考网 ...

随机推荐

  1. cmake 没有那个目录

    问题:bash: /usr/bin/cmake: 没有那个文件或目录 因为直接使用cmake系统回到默认的/usr/bin中去寻找,但是src中安装的cmake是在/usr/local/bin中,所以 ...

  2. Mac下Git安装及配置

    Mac下: 1.下载git版本并安装 运行终端 查看git版本: bogon:~ yan$ git --version git version 2.16.3 配置gitconfig文件 vim ~/. ...

  3. C++多态实现原理

    C++的多态性用一句话概括就是:在基类的函数前加上virtual关键字,在派生类中重写该函数,运行时将会根据对象的实际类型来调用相应的函数.如果对象类型是派生类,就调用派生类的函数:如果对象类型是基类 ...

  4. GNS3 1.4.0b3 MSTP多生成树配置实验

    一.实验目标 掌握MSTP多生成树配置,VLAN配置,trunk配置,etherchannel配置 二.实验平台 系统:WIN7以上windows,X64版本.CPU支持虚拟化,并在BIOS中开启虚拟 ...

  5. java表达式类型的自动提升

    当一个java算术表达式中包含多个基本类型的值时,整个算术表达式的数据类型将发生自动提升.Java定义如下的自动提升规则:1. 所有byte型.short型和char型将被提升到int型. 2. 整个 ...

  6. 网络协议 finally{ return问题 注入问题 jdbc注册驱动问题 PreparedStatement 连接池目的 1.2.1DBCP连接池 C3P0连接池 MYSQL两种方式进行实物管理 JDBC事务 DBUtils事务 ThreadLocal 事务特性 并发访问 隔离级别

    1.1.1 API详解:注册驱动 DriverManager.registerDriver(new com.mysql.jdbc.Driver());不建议使用 原因有2个: >导致驱动被注册2 ...

  7. vs2017 +CUDA 9.0配置

    环境: 1.Win7 64位 旗舰版 2.VS2017 3.CUDA 9.0 安装过程比较简单,直接运行在官网下载的CUDA安装包就可以了. 建议先安装VS,再安装CUDA.这样安装完之后会在VS里直 ...

  8. webpack4.x配置详解,多页面,多入口,多出口,新特性新坑!!

    花了差不多一天多的时间,重新撸了一遍webpack4.x的常用配置. 基本上常用的配置都熟悉了一遍,总体上来讲,为了对parcel进行反击,webpack从4.x开始,正在朝着尽可能的简化配置文件的方 ...

  9. 建立自己的git服务器

    需求情景 就像金山快盘同步盘那样, 在开发环境windows 10和部署环境Ubuntu server 14.04之间建立同步关系.比如windows端多了一个a.txt文件,你推送后,Ubuntu端 ...

  10. (译)快速指南:用UIViewPropertyAnimator做动画

    翻译自:QUICK GUIDE: ANIMATIONS WITH UIVIEWPROPERTYANIMATOR 译者:Haley_Wong iOS 10 带来了一大票有意思的新特性,像 UIViewP ...