ios本地推送

#import "AppDelegate.h"

 

@interface AppDelegate ()

 

@end

 

@implementation AppDelegate

 

//无论程序在前后台运行,只要接收到通知,就会调用此方法

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

{

    NSLog(@"%@",notification);

    //添加控件,接收显示到的信息

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

    label.backgroundColor=[UIColor grayColor];

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

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

    

}

 

 

 

 

 

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

    // Override point for customization after application launch.

    UILocalNotification *info=launchOptions[UIApplicationLaunchOptionsLocalNotificationKey];

    if (info) {

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

        label.backgroundColor = [UIColor orangeColor];

        

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

        

        

        

        //取出携带信息

        

        label.numberOfLines = 0;

        //NSDictionary *dict = info[@"UIConcreteLocalNotification"];

        // NSArray *keys = [info allKeys];

        //for (NSString *str in keys) {

        //   label.text = [NSString stringWithFormat:@"%@",str];

        //}

        //NSDictionary *dict1 = dict[@"user info"];

        

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

        //跳转对应的界面

    }

    

    

    

    

    return YES;

}

 

 

@end

#import "ViewController.h"

 

@interface ViewController ()

 

@end

 

@implementation ViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

    //iOS8之后 需要注册通知类型  包含哪些(声音,图标文字,文本) 信息

    /*

     UIUserNotificationTypeNone    = 0,

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

     UIUserNotificationTypeSound   = 1 << 1, // 声音      0010

     UIUserNotificationTypeAlert   = 1 << 2, // 主题内容  0100

     */

    

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

    //注册通知

    [[UIApplication sharedApplication]registerUserNotificationSettings:setting];

    

    

}

 

 

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

    //创建本地通知

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

    local.alertBody=@"老易:你为什么这么爱生气?";

    local.fireDate=[NSDate dateWithTimeIntervalSinceNow:3];

    local.soundName=UILocalNotificationDefaultSoundName;

    local.alertAction=@"";

    local.applicationIconBadgeNumber=5;

    local.userInfo=@{@"name":@"大易",@"age":@"18"};

    //定制通知

    [[UIApplication sharedApplication]scheduleLocalNotification:local];

//    //立即发送通知

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

 

//    //取消本地所有通知

//    [[UIApplication sharedApplication]cancelAllLocalNotifications];

    

    

    

    

    

    

    

    /*

     // timer-based scheduling 定制  特定的时间发出通知

     @property(nonatomic,copy) NSDate *fireDate; 触发 时间

     @property(nonatomic,copy) NSTimeZone *timeZone; 时区

     

     @property(nonatomic) NSCalendarUnit repeatInterval; 重复间隔     // 0 means don't repeat

     @property(nonatomic,copy) NSCalendar *repeatCalendar; 重复间隔

     

     @property(nonatomic,copy) CLRegion *region NS_AVAILABLE_IOS(8_0);//区域

     

     @property(nonatomic,assign) BOOL regionTriggersOnce NS_AVAILABLE_IOS(8_0); //决定区域的一个bool

     

     // alerts

     @property(nonatomic,copy) NSString *alertBody;  提醒的主题

     @property(nonatomic) BOOL hasAction;                // NO 不显示滑动解锁的按钮 反之 显示

     @property(nonatomic,copy) NSString *alertAction;    //滑动解锁的文字

     @property(nonatomic,copy) NSString *alertLaunchImage;   //点击通知横幅的启动程序的 启动 图片

     @property(nonatomic,copy) NSString *alertTitle   提示的标题文字

     

     // sound 默认: UILocalNotificationDefaultSoundName

     @property(nonatomic,copy) NSString *soundName;

     // badge

     @property(nonatomic) NSInteger applicationIconBadgeNumber;  //图标文字

     // user info

     @property(nonatomic,copy) NSDictionary *userInfo;   // 用户指定的携带参数

     [UIUserNotificationSettings settingsForUserNotificationTypes:userNotificationActionSettings:]

     @property (nonatomic, copy) NSString *category NS_AVAILABLE_IOS(8_0);分类

     */

}

@end

原文地址:https://www.cnblogs.com/tangranyang/p/4655839.html