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

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

最新文章

  1. Downgrade PHP 7 to PHP 5.6 on Ubuntu 16.04
  2. 关于 Java(TM) Platform SE binary 已停止工作 的解决方法
  3. zend studio中ctrl+鼠标左键无法转到类或函数定义文件的解决方法
  4. 一键QQ聊天与一键加群QQ功能
  5. 安卓第四天笔记-Sqlite
  6. Unity使用 UnityVS+VS2013 调试脚本
  7. form和validate示例
  8. Linux开发工具之Makefile(上)
  9. 应用之星在线app开发平台,菜鸟也会做应用
  10. UITableView 编辑模式(增加-删除-移动---自定义左滑 title)
  11. 搭建多系统yum服务器
  12. Duplicate column name &#39;vocabulary&#39;
  13. 关于dfs
  14. Excel 如何判断某列哪些单元格包含某些字符
  15. 【搬运】 Page Object 官方文档 (新增了Widget特性)
  16. angular点击查看更多(简单demo)
  17. python中的猴子补丁Monkey Patch
  18. dubbo-常用配置
  19. 进阶之路(基础篇) - 021 arduino基础知识
  20. Vote Disk 和 OCR概述

热门文章

  1. springboot解决跨域问题(Cors)
  2. 使用gogs搭建git私有仓库
  3. jQuery 遍历 – 同胞(siblings)
  4. 永远不要在循环之外调用wait方法
  5. Android广播的发送与接收
  6. 微信小程序基础之常用控件text、icon、progress、button、navigator
  7. ant编译mysql驱动
  8. 剑指Offer——常用SQL语句、存储过程和函数
  9. scheme深拷贝和浅拷贝探索
  10. Python 3 re模块3个括号相关的语法