点击本地通知和收到本地通知后区分

时间:2015-12-15 12:19:55

标签: ios uilocalnotification localnotification

我有应用程序发送本地通知。 所以我有两个场景。

  1. 应用程序位于前台,通知中心被拖出。通知被收回。通知中心正在被隐藏。

  2. 应用程序位于前台,通知中心被拖出。通知被收回。已选择通知。

  3. 我的handel通知代码

    的AppDelegate

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        UILocalNotification* notification = launchOptions[UIApplicationLaunchOptionsLocalNotificationKey];
    
        //When App is launched from notification
        [self setupLocalNotification:notification.userInfo];
    }
    
    -(void) application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
        NSLog(@"USER INFO %@",notification.userInfo);
        [self setupLocalNotification:notification.userInfo];
    }
    
    -(void)setupLocalNotification:(NSDictionary*)userInfo {
        UIApplicationState state = [[UIApplication sharedApplication] applicationState];
        if ( state == UIApplicationStateInactive || state == UIApplicationStateBackground){
            [[NSUserDefaults standardUserDefaults] setObject:userInfo forKey:@"localNotification"];
            //This is for situation when I'm deep in navigation controller and this also trigger viewWillAppear in ViewController
            [[((MainPanelViewController*)[[self window] rootViewController]) startNC] popToRootViewControllerAnimated:false];
        } else {
            //something
        }
    }
    

    ** MainPanelViewController **

    - (void)viewWillAppear:(BOOL)animated
    {
        [super viewWillAppear:animated];
    
        //This is for situation when currently in ViewController
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(beacomeActive) name:UIApplicationDidBecomeActiveNotification object:nil];
    }
    
    - (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [self checkLocalNotification];
    
    }
    
    - (void)viewWillDisappear:(BOOL)animated {
        [super viewWillDisappear:animated];
    
        [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidBecomeActiveNotification object:nil];
    }
    
    - (BOOL) checkLocalNotification{
        NSDictionary* userInfo = [[NSUserDefaults standardUserDefaults] objectForKey:@"localNotification"];
        if (userInfo != nil) {
            NSLog(@"Notification");
            [self performSegueWithIdentifier:@"ShowNotification" sender:self];
            return true;
        }
        return false;
    }
    

    问题 我不知道如何区分这两种情况。因为每当App处于非活动状态时收到通知,就会调用方法performSegueWithIdentifier

0 个答案:

没有答案