iOS9一下本地推送的代码实现


#import "ViewController.h"


/*

 // timer-based scheduling

 //触发时间

 @property(nullable, nonatomic,copy) NSDate *fireDate;

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

 

 @property(nonatomic) NSCalendarUnit repeatInterval;重复间隔

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

 

 // set a CLRegion object to trigger the notification when the user enters or leaves a geographic region, depending upon the properties set on the CLRegion object itself. registering multiple UILocalNotifications with different regions containing the same identifier will result in undefined behavior. the number of region-triggered UILocalNotifications that may be registered at any one time is internally limited. in order to use region-triggered notifications, applications must have "when-in-use" authorization through CoreLocation. see the CoreLocation documentation for more information.

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

 

 @property(nonatomic,assign) BOOL regionTriggersOnce NS_AVAILABLE_IOS(8_0);  进入区域之后的触发次数

 

 // alerts 提醒

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

 @property(nonatomic) BOOL hasAction; 锁屏界面的 滑动来解锁的按钮

 @property(nullable, nonatomic,copy) NSString *alertAction;   滑动来解锁的 字符串 聊天

 

 @property(nullable, nonatomic,copy) NSString *alertLaunchImage;   启动图片 点击横幅会启动应用

 @property(nullable, nonatomic,copy) NSString *alertTitle NS_AVAILABLE_IOS(8_2); 标题

 // sound

 @property(nullable, nonatomic,copy) NSString *soundName;      // name of resource in app's bundle to play or UILocalNotificationDefaultSoundName

 

 // badge

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

 

 // user info 用户信息

 @property(nullable, nonatomic,copy) NSDictionary *userInfo;

 

 // category identifer of the local notification, as set on a UIUserNotificationCategory and passed to +[UIUserNotificationSettings settingsForTypes:categories:]

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

 */

@interface ViewController ()


@end


@implementation ViewController


- (void)viewDidLoad {

    [superviewDidLoad];

   

    //本地推送


    //0.注册通知

    /*

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

     UIUserNotificationTypeBadge   = 1 << 0, 0001  //图标文字的修改

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

     UIUserNotificationTypeAlert   = 1 << 2, 0100 // 提示文字

     */

    

    //枚举特征

    //区分


    // iOS 6 之后苹果保护用户的权利 需要用户的授权

    //注册通知会弹出提示框让用户授权

    //QQ

    UIMutableUserNotificationCategory *category = [[UIMutableUserNotificationCategoryalloc]init];

    

    category.identifier = @"cateHello";

    

    

    UIMutableUserNotificationAction *action = [[UIMutableUserNotificationActionalloc]init];

    

    action.title = @"删除";

    action.identifier = @"delete";

    action.activationMode =UIUserNotificationActivationModeBackground;

    action.authenticationRequired =NO;

    action.destructive = YES;

    

    

    UIMutableUserNotificationAction *action1 = [[UIMutableUserNotificationActionalloc]init];

    action1.activationMode =UIUserNotificationActivationModeBackground;

    action1.title = @"标记已读";

    action1.authenticationRequired =YES;

    action1.identifier = @"tagReaded";

    action1.destructive = NO;

    

    //快捷回复

    // QQ  好的

    // 几点见?

    // 22:00 输入内容 文本框

    UIMutableUserNotificationAction *actionText = [[UIMutableUserNotificationActionalloc]init];

    actionText.behavior =UIUserNotificationActionBehaviorTextInput;

    


    actionText.identifier = @"actionText";

    

    // [category setActions:@[action,action1] forContext:UIUserNotificationActionContextDefault];

     [category setActions:@[actionText]forContext:UIUserNotificationActionContextDefault];

    

    UIUserNotificationSettings *setting = [UIUserNotificationSettingssettingsForTypes:UIUserNotificationTypeBadge |UIUserNotificationTypeSound | UIUserNotificationTypeAlertcategories:[NSSetsetWithObjects:category, nil]];


    [[UIApplicationsharedApplication]registerUserNotificationSettings:setting];

}

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

{

    //1.定制通知

    //1.1创建本地通知

    UILocalNotification *local = [[UILocalNotificationalloc]init];

    //1.2初始化赋值

    //5秒之后提醒

    local.fireDate = [NSDatedateWithTimeIntervalSinceNow:5];

    

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

    

    local.alertTitle = @"QQ";

    

    local.soundName =UILocalNotificationDefaultSoundName;

    

    local.applicationIconBadgeNumber =18;

    

    local.hasAction = YES;

    

    local.alertAction = @"聊天!";

    

    

    local.userInfo =@{@"QQ" : @"110" , @"name" :@"忘了爱" ,@"age"  :@18 ,@"room" : @"202"};

    

    local.category = @"cateHello";

    

    [[UIApplicationsharedApplication]scheduleLocalNotification:local];

    //2.展示通知

    //自动

    //代理  iOS设计模式

    

    //取消定制的通知

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

    //[[UIApplication sharedApplication]cancelAllLocalNotifications];

}

@end

+++================================================

#import "AppDelegate.h"

#import "CZChatViewController.h"

@interface AppDelegate ()


@end

static BOOL _isNeedJump;

@implementation AppDelegate


//接收通知跳转界面

//接收通知 ->点击横幅 (会执行方法)

//程序死了 从死到生 :didFinishLaunchingWithOptions

//1.点击图标  2.点击横幅 (launchOptions[UIApplicationLaunchOptionsLocalNotificationKey]有值)有通知

//程序活着 :

//1.后台

//会调用 didReceiveLocalNotification传递 通知对象给你

//2.前台没有横幅 默认接收通知  不需要跳转界面


//验证程序死了 是否执行某个方法



//接收到本地通知的时候就会调用   (点击横幅会接收)

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

{

    NSLog(@"didReceiveLocalNotification");

    //3.界面跳转 (聊天界面 ) (程序活着)

    // 什么时候点击了横幅 怎么跳转

    //点击横幅 ====就是接收了通知


    if ([[NSUserDefaultsstandardUserDefaults]objectForKey:@"isNeedJump"]) {

        CZChatViewController *vc =  [[UIStoryboardstoryboardWithName:@"chatVc"bundle:nil]instantiateInitialViewController];

        self.window.rootViewController = vc;

    }

    //明确跟谁聊天

    NSLog(@"%@",notification.alertBody);

    //拿到QQ唯一标识  查询的依据

    NSLog(@"%@",notification.userInfo);

    

    //没有关联 xcode   没有执行

    //程序死了之后确认是否执行了此方法的方式:

    //界面跳转 界面发生了变化

    //添加控件展示内容

//    UILabel *lable = [[UILabel alloc]init];

//    lable.backgroundColor = [UIColor redColor];

//    lable.frame = CGRectMake(100, 100, 100, 100);

//    [self.window.rootViewController.view addSubview:lable];

    //更改图标文字

//    [UIApplication sharedApplication].applicationIconBadgeNumber = 16;

}

//4.界面跳转 (聊天界面 ) (程序死了)

//没有打印 没有跳转




//程序本来死了  :  1.点击图标会调用  2.点击横幅(接收了通知)会调用 -> 跳转界面

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

    

    //点击横幅才需要

    if (launchOptions[UIApplicationLaunchOptionsLocalNotificationKey] ==nil) { //点击图标

        

    }else{

            CZChatViewController *vc =  [[UIStoryboardstoryboardWithName:@"chatVc"bundle:nil]instantiateInitialViewController];

     

        //对方的QQ

        UILocalNotification *local = launchOptions[UIApplicationLaunchOptionsLocalNotificationKey];

        UILabel *lable = [[UILabelalloc]init];

        lable.backgroundColor = [UIColoryellowColor];

        lable.text = [NSStringstringWithFormat:@"%@ %@ %@",[NSDatedate],launchOptions,local.userInfo];

        lable.frame = CGRectMake(100, 100, 500, 200);

        lable.numberOfLines = 0;

        [vc.view addSubview:lable];

        self.window.rootViewController = vc;

    }


    return YES;

}

//选择菜单按钮就会调用

- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void (^)())completionHandler

{

    if ([identifier isEqualToString:@"delete"]) {

        NSLog(@"点击了删除!");

    }else if ([identifierisEqualToString:@"tagReaded"]){

        NSLog(@"点击了已读!");

    }

              

    completionHandler();

}

//选择菜单按钮就会调用 如果实现了这个方法上面那个方法就不会调用

- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification withResponseInfo:(NSDictionary *)responseInfo completionHandler:(void (^)())completionHandler

{

    if ([identifier isEqualToString:@"delete"]) {

        NSLog(@"点击了删除!");

    }else if ([identifierisEqualToString:@"tagReaded"]){

        NSLog(@"点击了已读!");

    }else if ([identifierisEqualToString:@"actionText"]){

        NSLog(@"点击了 发送");

        NSLog(@"%@",responseInfo[UIUserNotificationActionResponseTypedTextKey]);

 

    }

    

    completionHandler();

}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值