应用终止后,使用NSNotification在UIViewController中处理UILocalNotification [英] Handle UILocalNotification in UIViewController using NSNotification after the app has been terminated
问题描述
我正在使用UILocalNotification
,我想通知我的一个控制器即使已终止该应用程序,也已收到通知.
I'm working with UILocalNotification
and I would like to notify one of my controller that the notification has been received even if the app has been terminated.
在我的appDelegate中,我实现了此功能:
In my appDelegate I implemented this function:
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
if ([application applicationState] == UIApplicationStateInactive) {
[[NSNotificationCenter defaultCenter] postNotificationName:@"localNotificationReceived" object:notification.userInfo];
}
}
在我的UIViewController
中,我在viewDidLoad
方法上实现了观察者
In my UIViewController
I implemented the observer on the viewDidLoad
method
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didlocalNotificationReceived:) name:@"localNotificationReceived" object:nil];
}
当应用程序在后台运行时,它可以完美运行.由于viewDidLoad
方法已经被调用并且观察者正在等待.
It works perfectly when the app run in background. Since the viewDidLoad
method has already been called and the Observer is waiting..
问题是我杀死该应用程序时.然后我的控制器的观察者消失了,并且从未调用didlocalNotificationReceived
方法.
The issue is when I kill the app. Then the observer of my controller is gone and the didlocalNotificationReceived
method is never called.
我认为这是因为当我收到localNotification
并再次运行该应用程序时. didReceiveLocalNotification:
方法在我的UIViewController
的viewDidLoad
之前调用.然后在PostNotificationName
之后创建观察者,然后观察者什么也没收到.
I think that it's because when I receive the localNotification
and run the app again. The didReceiveLocalNotification:
method is called before the viewDidLoad
of my UIViewController
. Then the observer is created after the PostNotificationName
then the observer receives nothing.
我想知道是否存在一些最佳实践或模式来处理此类问题.
I would like to know if there is some best practices or pattern to handle this kind of issue.
我知道didFinishLaunchingWithOptions
方法是在didlocalNotificationReceived
之前调用的,因此可能需要做一些事情.
I know that the didFinishLaunchingWithOptions
method is called before didlocalNotificationReceived
so there is probably something to do there.
更新:
我还发现应用何时终止.点击通知后,它会打开应用程序,调用函数didFinishLaunchingWithOptions
,但不要调用didReceiveLocalNotification
.因此,我认为我将以不同的方式处理这两种情况.
I also discovered that when is app is terminated. Once you tap the notification, It opens the app, call the function didFinishLaunchingWithOptions
but never call didReceiveLocalNotification
. So I think that I'm gonna handle both cases differently.
推荐答案
好,我找到了答案.
我实际上是手动初始化情节提要,在发布NSNotification
I actually, initialize manually my storyboard, and be cautious that I initialize my main view before posting the NSNotification
我的appDelegate
中的didFinishLaunchingWithOptions:
方法看起来像这样:
My didFinishLaunchingWithOptions:
method in my appDelegate
looks like that:
- (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:
方法中而不是在viewDidLoad:
Then in my UIViewController
I create the NSNotification
observer in the initWithCoder:
method instead of in 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:
方法:
And when the app is not killed I still use the didReceiveLocalNotification:
method:
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
if ([application applicationState] == UIApplicationStateInactive) {
[[NSNotificationCenter defaultCenter] postNotificationName:@"localNotificationReceived" object:notification.userInfo];
}
}
我不确定这是否是最佳做法.但是效果很好!
I'm not sure if it's the best practice. But it works well !
希望这会有所帮助:)
这篇关于应用终止后,使用NSNotification在UIViewController中处理UILocalNotification的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!