iOS-本地的推送

//

//  ViewController.m

//  05-本地的推送

//

//  Created by hongqiangli on 2017/6/12.

//

 

#import "ViewController.h"

 

@interface ViewController ()

 

@end

 

@implementation ViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

   

    

}

 

/*

 补充:

 让模拟器锁屏: command + l

 

 */

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

    //iOS8之后的改版,需要注册类型

    //声音,

    /*

     UIUserNotificationTypeNone    = 0,      // the application may not present any UI upon a notification being received

     UIUserNotificationTypeBadge   = 1 << 0, 包含图标文字

     UIUserNotificationTypeSound   = 1 << 1,声音

     UIUserNotificationTypeAlert   = 1 << 2,主题内容

     */

    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil];

    [[UIApplication sharedApplication]registerUserNotificationSettings:settings];

    

    //1 创建一个本地通知

    UILocalNotification *local = [[UILocalNotification alloc]init];

    //设置属性

    //设置推送内容

    local.alertBody = @"女神:在吗?";

    //设置从现在开始多少秒开始发这个推送

    local.fireDate = [NSDate dateWithTimeIntervalSinceNow:5];

    //设置声音(默认的)

    local.soundName = UILocalNotificationDefaultSoundName;

    //

    local.alertAction = @"";

    //文字

    local.applicationIconBadgeNumber = 10;

    //l

    local.userInfo = @{@"name": @"女神",@"content":@"在吗?"};

    //应用级别

    //立马发送通知

    [[UIApplication sharedApplication]presentLocalNotificationNow:local];

    //定制一个通知

//    [[UIApplication sharedApplication]scheduleLocalNotification:local];

    //取消

//    [[UIApplication sharedApplication]cancelAllLocalNotifications];

    //用代理,监听什么时候接收到通知

}

 

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

 

 

@end

 

 

//----------------

//

//  AppDelegate.m

//  05-本地的推送

//

//  Created by hongqiangli on 2017/6/12.

//

 

#import "AppDelegate.h"

 

@interface AppDelegate ()

 

@end

 

@implementation AppDelegate

 

//程序在前台的时候回调用接收到通知

//程序一直活着就会调用这个方法

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{

    

    NSLog(@"%@",notification);

}

 

//程序启动会调用:

//1 程序启动

//点击图标启动

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    UILocalNotification *info = launchOptions[UIApplicationLaunchOptionsLocalNotificationKey];

    if(info){

        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 300, 300)];

        label.backgroundColor = [UIColor redColor];

        label.numberOfLines = 0;

        //取出携带信息

//        NSDictionary *userInfo = info[@"userInfo"];

        label.text = [NSString stringWithFormat:@"%@",info.userInfo];

        [self.window.rootViewController.view addSubview:label];

        

        

    }

    

    return YES;

}

 

 

 

 

posted @ 2017-06-12 17:09  李洪强  阅读(243)  评论(0编辑  收藏  举报