[英]Open app through notification banner when app is not running state iOS
I have tabbar controller having three tab, attached three navigation controller I want to go to second controller of navigation controller just like whatsapp. 我有三个标签的选项卡控制器,附加了三个导航控制器,我想像whatsapp一样转到导航控制器的第二个控制器。 I handled successfully for background state, but for not running state.
我成功处理了后台状态,但没有运行状态。 Below is my code in didfinishlaunch delegate method of uiapplication.
下面是我在uiapplication的didfinishlaunch委托方法中的代码。
if (launchOptions != nil) {
// Launched from push notification
NSDictionary *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
[self performSelector:@selector(notificationObserverAfterDelay:) withObject:notification afterDelay:2.0];
}
-(void)notificationObserverAfterDelay:(NSDictionary *)userInfo
{
[[NSNotificationCenter defaultCenter] postNotificationName:@"RefreshUIChatNotRunningState" object:self userInfo:userInfo];
}
Nazish Ali, 纳兹什·阿里,
Sending out notification after delay of 2 seconds is really not a good idea. 延迟2秒后发送通知确实不是一个好主意。
Declare a method which accepts your userInfo dictionary as argument in your viewController having tab bar. 声明一个方法,该方法接受带标签栏的viewController中的userInfo字典作为参数。
lets call it as, 让我们称之为
YourViewController.h YourViewController.h
-(void)appStartedFromPushNotification : (NSDictionary *)userInfo;
YourViewController.m YourViewController.m
-(void)appStartedFromPushNotification : (NSDictionary *)userInfo {
//now the control has reached yourViewController with tab bar
//change the tab bar selection and perform segue and load the next viewController and use prepareforSgue to pass the data
}
Now why YourViewController only why not the actualViewController which anyway needs to be loaded ??? 现在,为什么要使用YourViewController而不是为什么不必须要加载的actualViewController? Reason you dont want mess with navigation stack manually.
您不希望手动弄乱导航堆栈的原因。 Because YourViewController is the child of UINavigationController and UINavigationController is your rootView controller YourViewController will be loaded automatically.
由于YourViewController是UINavigationController的子级,而UINavigationController是您的rootView控制器,因此YourViewController将自动加载。 So just access it and ask it to perform the tab change and load viewControllers so your navigation stack will continue to be stable :)
因此,只需访问它并要求它执行选项卡更改并加载viewControllers,以便您的导航堆栈将继续保持稳定:)
finally in Appdelegate, 终于在Appdelegate中
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSDictionary *userInfo = [launchOptions valueForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (launchOptions) {
[self handleReceiveRemoteNotification: userInfo];
}
return YES;
}
Declare a method in AppDelegate.m and access the YourViewController instance and handover data to it thats all :) 在AppDelegate.m中声明一个方法,然后访问YourViewController实例并将数据移交给这一切:)
- (void) handleReceiveRemoteNotification:(NSDictionary *)userInfo{
UINavigationController *rootViewController = (UINavigationController *)self.window.rootViewController;
for(UIViewController *viewController in rootViewController.viewControllers){
if([viewController isKindOfClass:[YourViewController class]]) {
[viewController appStartedFromPushNotification:userInfo];
}
}
That should do the job :) Happy coding :) 那应该做的工作:)快乐编码:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.