当应用被终止或未运行时,如何从iOS应用的推送通知中打开特定视图? [英] How to open specific view from Push notification of IOS app when app is killed or not running?
问题描述
我正在努力寻找在收到通知时打开IOS应用程序特定视图的解决方案,我想在用户点击收到的通知时打开屏幕.
I am struggling to find the solution for opening a specific view of my IOS app when i get notification, i want to open a screen when user taps on received notification.
我能够收到通知,并且当应用程序在后台运行并且我点击通知时,它会将我重定向到特定视图,当应用程序处于活动状态时它也可以工作,但是当我点击它和应用程序时,它只会打开应用程序处于被杀状态.
I am able to get the notification and when app is in background and i tap on notification it redirects me to specific view, it does also work when app is in active state but it just opens app when i tap on it and app is in killed state..
下面是我的appdelegate代码
below is my appdelegate code
if application.applicationState == .inactive{
print ("app is NOT active from not sec.")
let articleId = userInfo["notification_id"] as? String
UserDefaults.standard.set(articleId, forKey:"articleId");
let mainStoryboardIpad : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewControlleripad : UIViewController = mainStoryboardIpad.instantiateViewController(withIdentifier: "notificationsPageSB") as UIViewController
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.rootViewController = initialViewControlleripad
self.window?.makeKeyAndVisible()
let isPromoFCM = 1 as? Int
UserDefaults.standard.set(isPromoFCM, forKey:"isPromoFCM1");
}
else if application.applicationState == .active {
let articleId = userInfo["notification_id"] as? String
UserDefaults.standard.set(articleId, forKey:"articleId");
print ("app is active from not sec.")
let mainStoryboardIpad : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewControlleripad : UIViewController = mainStoryboardIpad.instantiateViewController(withIdentifier: "notificationsPageSB") as UIViewController
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.rootViewController = initialViewControlleripad
self.window?.makeKeyAndVisible()
/* let storyboard = UIStoryboard(name: "Main", bundle: nil);
let viewController: wow_Request_completed_vc = storyboard.instantiateViewController(withIdentifier: "wow_Request_completed_vc") as! wow_Request_completed_vc;
// Then push that view controller onto the navigation stack
let rootViewController = self.window!.rootViewController as! UINavigationController;
rootViewController.pushViewController(viewController, animated: true);
self.window?.rootViewController!.performSegue(withIdentifier: "link_to_wow_completed_vc", sender: nil)
*/
//let isOpenedThroughFCM = "yes"
//UserDefaults.standard.set(isOpenedThroughFCM, forKey:"isOpenedThroughFCM");
isComingFromFCM3 = 1;
} else {
// let isOpenedThroughFCM = "yes"
// UserDefaults.standard.set(isOpenedThroughFCM, forKey:"isOpenedThroughFCM");
isComingFromFCM3 = 1;
print ("app is NOT active from not sec.")
let articleId = userInfo["notification_id"] as? String
UserDefaults.standard.set(articleId, forKey:"articleId");
let mainStoryboardIpad : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewControlleripad : UIViewController = mainStoryboardIpad.instantiateViewController(withIdentifier: "notificationsPageSB") as UIViewController
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.rootViewController = initialViewControlleripad
self.window?.makeKeyAndVisible()
}
推荐答案
按照我的想法,让我们尝试检查
As my idea, let try to check [UIApplication sharedApplication].applicationIconBadgeNumber
value in
- (void)applicationDidBecomeActive:(UIApplication *)application
您可以确定在此处导航某处
you can determine navigate somewhere or something here
更新:我的答案是Objective-C,但是您可以申请Swift:p
UPDATE: my answer is Objective-C, but you can apply to Swift :p
这篇关于当应用被终止或未运行时,如何从iOS应用的推送通知中打开特定视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!