在应用程序终止后,使用NSNotification在UIViewController中处理UILocalNotifica



我正在使用UILocalNotification,我想通知我的一个控制器,即使应用程序已终止,也已收到通知。

在我的appDelegate中,我实现了以下功能:

-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
    if ([application applicationState] == UIApplicationStateInactive) {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"localNotificationReceived" object:notification.userInfo];
    }
}

在我的UIViewController中,我在viewDidLoad方法上实现了观测器

- (void)viewDidLoad
{
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didlocalNotificationReceived:) name:@"localNotificationReceived" object:nil];   
}

当应用程序在后台运行时,它可以完美工作。由于已经调用了viewDidLoad方法,并且Observer正在等待。。

问题是当我终止应用程序时。然后,我的控制器的观测器就消失了,didlocalNotificationReceived方法也从未被调用。

我想这是因为当我收到localNotification并再次运行应用程序时。didReceiveLocalNotification:方法在我的UIViewControllerviewDidLoad之前调用。然后在CCD_ 10之后创建观察者,然后观察者什么也没有接收到。

我想知道是否有一些最佳实践或模式来处理这类问题。

我知道didFinishLaunchingWithOptions方法是在didlocalNotificationReceived之前调用的,所以可能有一些事情要做。

更新:

我还发现当应用程序被终止时。点击通知后,它会打开应用程序,调用函数didFinishLaunchingWithOptions,但从不调用didReceiveLocalNotification。所以我想我会以不同的方式处理这两种情况。

好的,我找到了答案。

实际上,我手动初始化了我的故事板,并注意在发布NSNotification 之前初始化我的主视图

appDelegate中的didFinishLaunchingWithOptions:方法如下所示:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
    UIViewController *vc =[storyboard instantiateInitialViewController]; //call the initWithCoder: method of my controller
    if (launchOptions[UIApplicationLaunchOptionsLocalNotificationKey]) {
        UILocalNotification *localNotification = launchOptions[UIApplicationLaunchOptionsLocalNotificationKey];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"localNotificationReceived" object:localNotification.userInfo];
    }
    self.window.rootViewController = vc;
    [self.window makeKeyAndVisible];
    return YES;
}

然后在我的UIViewController中,我在initWithCoder:方法中创建了NSNotification观察器,而不是在viewDidLoad:

- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didlocalNotificationReceived:) name:@"localNotificationReceived" object:nil];
    }
    return self;
}
- (void)didlocalNotificationReceived:(NSNotification *)notification
{
    //Execute whatever method when received local notification
}

当应用程序没有被杀死时,我仍然使用didReceiveLocalNotification:方法:

-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
    if ([application applicationState] == UIApplicationStateInactive) {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"localNotificationReceived" object:notification.userInfo];
    }
}

我不确定这是否是最好的做法。但效果很好!

希望它能有所帮助:)

最新更新