ios7 本地通知

//
//  XXBViewController.m
//  2014_11_07_本地同时_ios7
//
//  Created by Mac10.9 on 14-11-7.
//  Copyright (c) 2014年 xiaoxiaobing. All rights reserved.
//

#import "XXBViewController.h"

@interface XXBViewController ()

@end

@implementation XXBViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self createLocalNotification];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    /**
     *  获取本地的所有通知 如果没有的画就添加通知 如果有得话就取消所有通知
     */
    NSArray *locNotis = [UIApplication sharedApplication].scheduledLocalNotifications;
    if (locNotis.count > 0)
    {
        //添加通知
        [self  createLocalNotification];
    }
    else
    {
        //取消通知
        [self removeLocalNotification];
    }
}
/**
 *  建立一个本地通知
 */
- (void)createLocalNotification
{
    NSLog(@"添加本地通知");
    // 本地推送
    //一个本地通知就是一个任务
    // 1.创建 本地通知对象
    UILocalNotification *localNoti = [[UILocalNotification alloc] init];
    // 2.通知提醒的时间 多少时间以后提醒
    localNoti.fireDate = [NSDate dateWithTimeIntervalSinceNow:1];
    //设置时区 默认时区就好跟 defaultTimeZone
    localNoti.timeZone = [NSTimeZone defaultTimeZone];
    // 3.设置通知的内容
    localNoti.alertBody = @"女友的生日到了";
    // 4.设置锁屏的时显示消息
    localNoti.alertAction = @"xxxx";
    // 5.设置 点通知后 启动程序时的图片 默认情况下没有 应该设置成和程序的启动界面一样
    localNoti.alertLaunchImage = @"LaunchImage";
    // 6.设置图标右上角数字 马上就有 只会执行一次 不管通知多烧毁都只会执行一次
    localNoti.applicationIconBadgeNumber ++;
    // 7.设置通知重复的间隔 最好不要重复 不会
    localNoti.repeatInterval = NSCalendarUnitSecond;
    // 8.添加额外的信息 可以在userInfo中找到
    localNoti.userInfo = @{@"detail": @"中午送花到公司 晚上烛光晚餐"};
    // 执行本地通知
    [[UIApplication sharedApplication] scheduleLocalNotification:localNoti];
}
/**
 *  取消所有的通知
 */
- (void)removeLocalNotification
{
    NSLog(@"取消通知");
    [[UIApplication sharedApplication] cancelAllLocalNotifications];
}
@end



//
//  XXBAppDelegate.m
//  2014_11_07_本地同时_ios7
//
//  Created by Mac10.9 on 14-11-7.
//  Copyright (c) 2014年 xiaoxiaobing. All rights reserved.
//

#import "XXBAppDelegate.h"


@interface XXBAppDelegate ()

@property (nonatomic, strong) UILocalNotification *locNotifi;

@end

@implementation XXBAppDelegate

/**
 *  如果程序被杀掉了 通知的相关信息可以在这里获得
 */
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    if (launchOptions) {
        // 获取本地通知对象
        UILocalNotification *localNot = launchOptions[UIApplicationLaunchOptionsLocalNotificationKey];
        // 有本地通知,才要显示
        if (localNot) {
            [self showLocalNotif:localNot];
        }
       
    }

    return YES;
}
/**
 *  如果程序进入后台了所有的通知可以再这里获得
 */
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
    NSLog(@"%@",notification.userInfo);
    [self showLocalNotif:notification];
}
/**
 * 显示一个提示窗口
 */
-(void)showLocalNotif:(UILocalNotification *)notification{
    NSLog(@"%@",notification.alertBody);
    self.locNotifi = notification;
    UIAlertView *alert  = [[UIAlertView alloc] initWithTitle:@"提醒" message:notification.alertBody delegate:self cancelButtonTitle:@"知道了" otherButtonTitles:@"稍微后再提醒", nil];
    [alert show];
}

@end


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值