iOS学习 本地推送与远程推送

本地推送:

#import "AppDelegate.h"

#import "CZChatViewController.h"


@interface AppDelegate ()


@end


@implementation AppDelegate

//跳转到聊天界面

//程序死了: 接收通知  -> didFinishLaunchingWithOptions (跳转界面的处理)

//程序活着: 接收通知  -> didReceiveLocalNotification (跳转界面的处理)


//接收到本地通知之后就会调用

//程序活着: 1.前台  2.后台

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

{

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

    //跳转界面  (QQ 跳转到聊天界面)

    

    //怎么验证是否来了?

    //调试

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

    label.numberOfLines= 0;

    label.backgroundColor = [UIColor redColor];

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

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

    

    //跳转界面(聊天界面)

    //冲突

    //避免冲突   : 1.沟通 修改公共文件时告诉别人 2.分模块  //3.纯代码(可以,自动布局比较痛苦)

    UIStoryboard *story = [UIStoryboard storyboardWithName:@"chat" bundle:nil];

    

    CZChatViewController *chat = story.instantiateInitialViewController;

    

    self.window.rootViewController = chat;    

}


//从死到生 会来到didFinishLaunchingWithOptions

//1.点击图标(启动)

//2.接收通知(程序死了) : launchOptions[UIApplicationLaunchOptionsLocalNotificationKey] 有值

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

    if (launchOptions[UIApplicationLaunchOptionsLocalNotificationKey]) { //接收通知进来

        //跳转界面  (QQ 跳转到聊天界面)

        //怎么验证是否来了?

        //调试方式 添加控件

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

        label.numberOfLines= 0;

        label.backgroundColor = [UIColor purpleColor];

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

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

        

        //跳转界面(聊天界面)        

        UIStoryboard *story = [UIStoryboard storyboardWithName:@"chat" bundle:nil];        

        CZChatViewController *chat = story.instantiateInitialViewController;        

        self.window.rootViewController = chat;

    }

    return YES;

}



//本地通知的 快捷回复的处理响应

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

{    

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

        NSLog(@"接收信息!");

        

    }else if ([identifier isEqualToString:@"nihao222"]) {

        

        NSLog(@"拒绝!!");

        

    }    

    //系统的回调

    completionHandler();    

}



#import "ViewController.h"


@interface ViewController ()


@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];


    /*

     // timer-based scheduling定制

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

    

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

     

     @property(nonatomic) NSCalendarUnit repeatInterval;      // 0 means don't repeat

     @property(nullable, nonatomic,copy) NSCalendar *repeatCalendar;

     

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

     

     // when YES, the notification will only fire one time. when NO, the notification will fire every time the region is entered or exited (depending upon the CLRegion object's configuration). default is YES.

     @property(nonatomic,assign) BOOL regionTriggersOnce NS_AVAILABLE_IOS(8_0);

     

     // alerts

     @property(nullable, nonatomic,copy) NSString *alertBody;  @"11111"

     @property(nonatomic) BOOL hasAction;                // defaults to YES. pass NO to hide launching button/slider

     @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; 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;

     */

    

    //0.注册通知类型

    /*

     //试试  <<  就可以拼接 同时具备

     UIUserNotificationTypeNone    = 0,

     UIUserNotificationTypeBadge   = 1 << 0, 图标文字

     UIUserNotificationTypeSound   = 1 << 1, 声音

     UIUserNotificationTypeAlert   = 1 << 2, 提示文字

     

     0000

     0001

     0010

     0100

     

     0101

     

     */

    

    //快捷回复

    

    UIMutableUserNotificationCategory *category = [[UIMutableUserNotificationCategory alloc]init];

    

    //唯一标示

    category.identifier = @"hello";


    /*

     // The unique identifier for this action.

     @property (nullable, nonatomic, copy) NSString *identifier;

     

     // The localized title to display for this action.

     @property (nullable, nonatomic, copy) NSString *title; 标题

     

     @property (nonatomic, assign) UIUserNotificationActionBehavior behavior NS_AVAILABLE_IOS(9_0);

     

     // Parameters that can be used by some types of actions.

     @property (nonatomic, copy) NSDictionary *parameters NS_AVAILABLE_IOS(9_0);

     

     //枚举

     @property (nonatomic, assign) UIUserNotificationActivationMode activationMode;

     

     //按钮响应事件 的处理 是否需要打开APP

     UIUserNotificationActivationModeForeground, //程序前台 打开APP

     UIUserNotificationActivationModeBackground  //后台 陌陌的处理

     @property (nonatomic, assign, getter=isAuthenticationRequired) BOOL authenticationRequired;  是否解锁屏幕的BOOl

     

     // Whether this action should be indicated as destructive when displayed.

     //是否显示 红色字体

     @property (nonatomic, assign, getter=isDestructive) BOOL destructive;


     */

    //Action 动作

    UIMutableUserNotificationAction *action1 = [[UIMutableUserNotificationAction alloc]init];

    action1.identifier = @"nihao111";

    action1.title = @"好的000";

    action1.activationMode = UIUserNotificationActivationModeBackground;

    action1.authenticationRequired = NO;

    action1.destructive = YES;

    

    

    UIMutableUserNotificationAction *action2 = [[UIMutableUserNotificationAction alloc]init];

    action2.identifier = @"nihao222";

    action2.title = @"不好000";

    action2.activationMode = UIUserNotificationActivationModeBackground;

    action2.authenticationRequired = NO;

    action2.destructive = NO;

    

    //UIUserNotificationActionContextDefault 默认可以添加四个动作

    [category setActions:@[action1,action2] forContext:UIUserNotificationActionContextDefault];

    

    NSSet *set = [NSSet setWithObjects:category, nil];

    

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

    

    [[UIApplication sharedApplication]registerUserNotificationSettings:setting];

    

}

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

{

    //1.创建本地通知  特定的时间显示出来 (定制)

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

    

    //2.初始化  属性

    local.alertBody = @"女神: 202";

    local.alertAction = @"聊天";

    local.fireDate = [NSDate dateWithTimeIntervalSinceNow:5];

    local.applicationIconBadgeNumber = 10;

    local.category = @"hello";

    local.userInfo = @{@"name" : @"萌妹子" , @"age" : @17 ,@"QQ" : @"137"};

    

    

    //3.定制  使用

    [[UIApplication sharedApplication]scheduleLocalNotification:local];

    

    //4.知道啥时候接收了通知

    //代理

   

}



远程推送:

#import "AppDelegate.h"


@interface AppDelegate ()


@end


@implementation AppDelegate


//程序启动  程序接收远程通知  程序死了

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

    

    UIMutableUserNotificationAction *jieshou = [[UIMutableUserNotificationAction alloc]init];

    jieshou.identifier = @"jieshou111";

    jieshou.title = @"接受";

    jieshou.activationMode = UIUserNotificationActivationModeBackground;

    jieshou.destructive = NO;

    

    

    

    UIMutableUserNotificationAction *reject = [[UIMutableUserNotificationAction alloc]init];

    reject.identifier = @"jujue111";

    reject.title = @"拒绝";

    reject.activationMode = UIUserNotificationActivationModeBackground;

    reject.destructive = YES;

    

    

    

    UIMutableUserNotificationCategory *category = [[UIMutableUserNotificationCategory alloc]init];

    

    category.identifier = @"xiaozhang";

    

    [category setActions:@[jieshou,reject] forContext:UIUserNotificationActionContextDefault];

    

    //0.注册通知类型

    UIUserNotificationSettings *setting = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadgeUIUserNotificationTypeAlert | UIUserNotificationTypeSound categories:[NSSet setWithObjects:category, nil]];

    

    [[UIApplication sharedApplication]registerUserNotificationSettings:setting];

    

    //1.定制远程通知

    // 1.1 上传UDID+ bundleID = deviceToken

    [[UIApplication sharedApplication]registerForRemoteNotifications];

    

    //代理  block 返回值  地址的参数    

    if (launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey]) {

        NSLog(@"跳转界面!!!");

    }

    return YES;

}



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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值