iOS本地通知(NSLocalNotification)的使用

效果图:




图一:


核心代码:

//
//  JLViewController.m
//  本地通知
//
//  Created by XinYou on 15-5-15.
//  Copyright (c) 2015年 vxinyou. All rights reserved.
//

#import "JLViewController.h"

@interface JLViewController ()
- (IBAction)addLocalNotification;


@end

@implementation JLViewController

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


/*
 三种情况:
 1.应用没有被关闭,而是在后台运行着。
    此时点击通知进入应用,会调用JLAppDelegate的didReceiveLocalNotification方法,
    而且如果有设置notification的alertLaunchImage属性,会先显示该属性对应的图片,然后进入应用。
 
 2.应用处于关闭状态,没有在后台运行。
    点击应用图标启动应用,会调用JLAppDelegate的didFinishLaunchingWithOptions方法
 
 3.应用处于关闭状态,没有在后台运行。
    点击通知启动应用,也会调用JLAppDelegate的didFinishLaunchingWithOptions方法
 
 那么,该如何区分是通过 点击应用图标 还是通过 点击通知 进入应用的呢?
 具体参见JLAppDelegate的didFinishLaunchingWithOptions方法
 
 */



/**
 *  添加本地通知
 */
- (IBAction)addLocalNotification {

    // 1.创建通知
    UILocalNotification *notification = [[UILocalNotification alloc] init];
    
    // 2.设置属性
    
    // 下面这句代码的作用是:锁屏的时候会显示“滑动来开始玩游戏”,如图一所示。
    notification.alertAction = @"开始玩游戏";
    // 通知的内容
    notification.alertBody = @"屠龙宝刀,点击就送!!!";
    
    // 通知对应的提醒数字。当通知来临,会在应用图标的右上角有一个提醒数字
    notification.applicationIconBadgeNumber = 1;
    
    // 通知的重复时间间隔(枚举类型)。这里设置为每一个小时通知一次
//    notification.repeatInterval = NSCalendarUnitHour;
    
    // 一般设置为程序的启动图片(LaunchImage)。意思是通过点击通知来重新进入应用时,需要显示的图片。
//    notification.alertLaunchImage = @"Default";
    
    // 通知时间。设定为4秒之后显示通知
    notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:4.0];
    
    // 这里设置的userinfo,可以在JLAppDelegate.m文件中的didFinishLaunchingWithOptions方法中获得。
    notification.userInfo = @{@"username" : @"zhangsan"};
    
    // 3.注册通知
    
    UIApplication *application = [UIApplication sharedApplication];
    
    // 先取消通知,避免重复添加
    [application cancelLocalNotification:notification];
    
    // 取消所有通知
//    [application cancelAllLocalNotifications];
    
    // 注册(添加)通知
    [application scheduleLocalNotification:notification];
}

@end
//
//  JLAppDelegate.m
//  本地通知
//
//  Created by XinYou on 15-5-15.
//  Copyright (c) 2015年 vxinyou. All rights reserved.
//

#import "JLAppDelegate.h"

@implementation JLAppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // 如果launchOptions为空,表示是通过点击应用图标的方式启动程序
    if (launchOptions == nil) {
        
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"正常启动应用" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        
        [alertView show];
        
    }else{// 如果launchOptions不为空,表示通过点击通知的方式启动程序
    
        UILocalNotification *notification = launchOptions[UIApplicationLaunchOptionsLocalNotificationKey];
        
        NSDictionary *userinfo = notification.userInfo;
        
        NSString *username = userinfo[@"username"];
        
        NSString *msg = [NSString stringWithFormat:@"点击通知启动应用!username:%@", username];
        
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:msg delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        
        [alertView show];
    }
    
    return YES;
}

/**
 *  用户点击通知进入本应用(应用并没有被关闭,而是在后台运行着)
 */
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{

    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"点击通知进入应用" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    
    [alertView show];
}
							
- (void)applicationWillResignActive:(UIApplication *)application
{
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application
{
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end

源码下载地址:https://github.com/liujie537192/LocalNotification

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值