ios本地通知

ios本地通知_第1张图片


什么时候需要推送跳转

1, 程序在后台(用户如果在app内,此时是不能推送的)
2, 程序被杀死,此时通过didReceiveLocalNotification监听不到,我们需要在程序的didFinishLaunchingWithOptions当中判断     launchOptions[UIApplicationLaunchOptionsLocalNotificationKey]

//
//  ViewController.m
//  LocalNotification
//
//  Created by hq on 16/5/12.
//  Copyright © 2016年 hanqing. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

- (IBAction)sendNoty:(UIButton *)sender;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

}

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

//在这里发送一个本地通知
- (IBAction)sendNoty:(UIButton *)sender {
    
    UILocalNotification *notification=[[UILocalNotification alloc]init];
    
    //设置推送的时间,5秒之后执行该推送
    notification.fireDate=[NSDate dateWithTimeIntervalSinceNow:5.0];
    
    //通知的标题
    
    notification.alertTitle=@"我的账本";
    
    //通知内容
    notification.alertBody=@"明天记得信用卡还款";
    
    //设置图标上小红点的数量
    notification.applicationIconBadgeNumber=1;
    
    //通过通知启动时代app启动图,这里我们随便写上一个,到时候程序会使用我们的默认启动图
    notification.alertLaunchImage=@"lllllasd";
    
    //设置推送声音,可以自定义声音@"a.wav"
    notification.soundName=UILocalNotificationDefaultSoundName;
    
    //设置其他的额外信息
    notification.userInfo=@{@"type":@"chatPage"};
    
    //调用通知
    //还需要在我们的appdelegate当中注册该通知才行
    [[UIApplication sharedApplication] scheduleLocalNotification:notification];

}
@end

//
//  AppDelegate.m
//  LocalNotification
//
//  Created by hq on 16/5/12.
//  Copyright © 2016年 hanqing. All rights reserved.
//

#import "AppDelegate.h"
#import "HQChatPageController.h"

@interface AppDelegate ()

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    //在这里注册我们的通知
    if ([[UIDevice currentDevice].systemVersion doubleValue]>=8.0) {

        UIUserNotificationSettings *settings=[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge| UIUserNotificationTypeSound|UIUserNotificationTypeAlert categories:nil];
        
        [application registerUserNotificationSettings:settings];
    }
    
    //处理程序被杀死后,不能通过通知跳转到指定页面的问题
    
    //如果时通过通知启动的
    if (launchOptions[UIApplicationLaunchOptionsLocalNotificationKey]) {
        
        NSLog(@"程序被杀死,通过通知启动app");
        
        UILocalNotification *notification=[launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
        
        //如果通知的类型时chatPage,则跳转到聊天界面
        if ([notification.userInfo[@"type"] isEqualToString:@"chatPage"]) {
            
            UILabel *redView = [[UILabel alloc] init];
            redView.frame = CGRectMake(0, 0, 200, 300);
            redView.numberOfLines = 0;
            redView.font = [UIFont systemFontOfSize:12.0];
            redView.backgroundColor = [UIColor redColor];
            redView.text = [NSString stringWithFormat:@"%@", notification.userInfo[@"type"]];
            
            [self.window.rootViewController.view addSubview:redView];
            
            HQChatPageController *chatVC=[[HQChatPageController alloc]init];
            
            UINavigationController *navVC=(UINavigationController *)application.keyWindow.rootViewController;
            
            [navVC pushViewController:chatVC animated:YES];
            
        }
        
        //直接调用我们的通知接收对象
        //[self application:application didReceiveLocalNotification:notification];
    }
    
    return YES;
}


//应用程序在前台,应用程序进入前台都会调用该方法

//杀了进程,通过通知栏跳转到指定界面实效,因此我们需要在程序启动的时候didFinishLaunchingWithOptions再处理一下


-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{
    
    NSLog(@"程序在前台,没被杀死监听");
    
    //首先需要判断当前用户是否在app内,如果在到话,则不跳转
    if(application.applicationState==UIApplicationStateInactive){
        
        //如果通知的类型时chatPage,则跳转到聊天界面
        if ([notification.userInfo[@"type"] isEqualToString:@"chatPage"]) {
            
            HQChatPageController *chatVC=[[HQChatPageController alloc]init];
            
            UINavigationController *navVC=(UINavigationController *)application.keyWindow.rootViewController;
            
            [navVC pushViewController:chatVC animated:YES];
            
        }
        
    }
}


- (void)applicationWillResignActive:(UIApplication *)application {
    
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    
    //清空我们的icon小红点
    [application setApplicationIconBadgeNumber:0];

}

- (void)applicationWillTerminate:(UIApplication *)application {
    
    
}

@end





你可能感兴趣的:(ios本地通知)