zoukankan      html  css  js  c++  java
  • 本地推送通知界面跳转demo

    本地推送通知界面跳转demo:

    /*
       1.在发送本地通知的时候,通过userInfo属性来指示跳转到那个界面
       
       2.监听本地通知的接收
             1.当收到本地通知就会调用该代理方法
             调用场景
             1.如果应用程序在后台,当点击通知的时候
             2.如果应用程序在前台,一旦收到本地通知,就会调用该方法 
               所以:判断如果应用程序在前台时候,不要执行界面跳转,来提高用户体验
             3.如果应用程序被杀死了,那么这个方法就不再执行了
    
            //- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
    
    //  3.在 didFinishLaunchingWithOptions:方法中,完成界面跳转
        判断用户是点击本地通知进入到应用的
        1.从应用程序的加载项取出本地通知对象
         UILocalNotification *ln  = launchOptions[UIApplicationLaunchOptionsLocalNotificationKey];
        2.如果 ln 有值表示是用户点击本地通知进入的,这个时候,才需要进行界面跳转.
    
     */
    
    #import "AppDelegate.h"
    
    @interface AppDelegate ()
    
    @end
    
    @implementation AppDelegate
    
    
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        // Override point for customization after application launch.
    //  如果应用程序被杀死了,我们需要在这里处理界面跳转
        
    //  1.从应用程序的加载项取出本地通知对象
        UILocalNotification *ln  = launchOptions[UIApplicationLaunchOptionsLocalNotificationKey];
        
    //  2.如果存在这个对象,表示用户是点击本地通知进入到应用的
        if (ln) {
    //      跳转界面
            [self jumpToPageWithLocalNotification:ln];
        }
    
        return YES;
    }
    
    
    /**
      1.当收到本地通知就会调用该代理方法
      调用场景
      1.如果应用程序在后台,当点击通知的时候
      2.如果应用程序在前台,一旦收到本地通知,就会调用该方法
      3.如果应用程序被杀死了,那么这个方法就不再执行了
     */
    - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
    {
        /*
         UIApplicationStateActive,    活动
         UIApplicationStateInactive,  活动与后台切换的一种状态
         UIApplicationStateBackground 后台
         */
    //  如果应用在前台,就不要跳转界面了
        if (application.applicationState == UIApplicationStateActive) {
            return;
        }
    //  跳转界面
        [self jumpToPageWithLocalNotification:notification];
    
    }
    
    ///跳转界面
    - (void) jumpToPageWithLocalNotification:(UILocalNotification *) notification
    {
        //    NSLog(@"%s",__func__);
        //  取出用户信息
        NSDictionary *userInfo = notification.userInfo;
        //  拿到跳转到索引
        NSInteger index = [userInfo[@"index"] integerValue];
        
        //  获取根控制
        UITabBarController *tabVc   = (UITabBarController *)self.window.rootViewController;
        
        //  跳转界面
        tabVc.selectedIndex = index;
    }
    
    
    @end
    #import "ViewController.h"
    
    @interface ViewController ()
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
    //  取消所有通知
        [[UIApplication sharedApplication] cancelAllLocalNotifications];
        
    //  在iOS8之后,Apple对用户隐私要求更加严格,所有很多东西都需要请求用户权限
        
        if ([UIDevice currentDevice].systemVersion.doubleValue >= 8.0) {
            
            /*
             
             UIUserNotificationTypeNone    = 0,      // 不展示通知
             UIUserNotificationTypeBadge   = 1 << 0, // 应用图标右上角的红色数字权限
             UIUserNotificationTypeSound   = 1 << 1, // 提示音的权限
             UIUserNotificationTypeAlert   = 1 << 2, // 弹窗或横幅的权限
             */
    //       权限类型
            UIUserNotificationType types = UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlert;
    //      用户通知设置
            UIUserNotificationSettings *setting = [UIUserNotificationSettings settingsForTypes:types  categories:nil];
    //      注册用户设置,用户的请求窗口会弹出一次
            [[UIApplication sharedApplication] registerUserNotificationSettings:setting];
        }
        
        
    }
    
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
    
    }
    
    
    - (IBAction)jumpFriend {
        
        
        NSDictionary *userInfo = @{@"index":@(2)};
        
        [self sendLocalNotification:@"跳转到好友" andUserInfo:userInfo];
    }
    - (IBAction)jumpSesssion {
        
        NSDictionary *userInfo = @{@"index":@(1)};
        
        [self sendLocalNotification:@"跳转到朋友圈" andUserInfo:userInfo];
    }
    
    
    - (void) sendLocalNotification:(NSString *) alertBody andUserInfo:(NSDictionary *) userInfo
    {
        //  1.创建本地通知对象
        UILocalNotification *ln = [[UILocalNotification alloc] init];
        
        //  2.设置本地通知属性
        //************************************************************/
        //  什么时候发送这个本地通知
        ln.fireDate = [NSDate dateWithTimeIntervalSinceNow:5];
        //  设置通知的内容
        ln.alertBody = alertBody;
        //  设置提示音,如果用户是静音,它自动会转为震动
        ln.soundName = UILocalNotificationDefaultSoundName;
        //  应用图标
        ln.applicationIconBadgeNumber = 10;
        //  当用用户点击通知时候,进入应用程序,要不要显示启动图标
        ln.alertLaunchImage = @"UILaunchImageFile";
    //  用户信息(比如界面跳转的信息)
        ln.userInfo = userInfo;
        //************************************************************/
    
        //  调度本地通知
        [[UIApplication sharedApplication] scheduleLocalNotification:ln];
    }
    
    
    
    @end
  • 相关阅读:
    全文索引的书
    图片上传预览
    sqlserver 递归删除组织结构树
    dataset 转泛型list
    jquery easyui tree 异步加载数据
    sqlserver 禁用外键
    linx 实用操作命令二
    Compilation failed: this version of PCRE is not compiled with PCRE_UTF8 support at offset 0
    Centos linux php扩展安装步骤
    linux Apache和php配置
  • 原文地址:https://www.cnblogs.com/ZMiOS/p/5074295.html
Copyright © 2011-2022 走看看