gpt4 book ai didi

ios - 退出应用程序时从远程通知显示 uiviewcontroller

转载 作者:搜寻专家 更新时间:2023-10-31 22:50:17 24 4
gpt4 key购买 nike

当我收到远程推送通知时,我试图显示一个特定的 View Controller 。我已将所有代码添加到方法 didReceiveRemoteNotification 中:

func application(application: UIApplication, didReceiveRemoteNotification userinfo: [NSObject: AnyObject])

我添加了以下代码:

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
let code = (userInfo["aps"] as! [String: AnyObject])
// Call to retrieve blog
if let blog = code["b"] as? NSNumber {
let blogId = blog as! Int

// Show blog from notification
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main",bundle: nil)
var controller = mainStoryboard.instantiateViewControllerWithIdentifier("blogCtrl") as! BlogController
controller.blogId = blogId
var rootController = mainStoryboard.instantiateViewControllerWithIdentifier("navCtrl1") as! UINavigationController
self.window?.rootViewController = rootController
rootController.pushViewController(controller, animated: true)
self.window?.makeKeyAndVisible()
}
if let tonic = code["t"] as? NSNumber {
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main",bundle: nil)
var controller = mainStoryboard.instantiateViewControllerWithIdentifier("tonicDetail") as! TonicDetailController
controller.tonicId = tonic as! Int
var rootController = mainStoryboard.instantiateViewControllerWithIdentifier("navCtrl1") as! UINavigationController
self.window?.rootViewController = rootController
rootController.pushViewController(controller, animated: true)
self.window?.makeKeyAndVisible()
}
if let gin = code["g"] as? NSNumber {
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main",bundle: nil)
var controller = mainStoryboard.instantiateViewControllerWithIdentifier("GinDetail") as! GinDetailController
controller.ginId = gin as! Int
var rootController = mainStoryboard.instantiateViewControllerWithIdentifier("navCtrl1") as! UINavigationController
self.window?.rootViewController = rootController
rootController.pushViewController(controller, animated: true)
self.window?.makeKeyAndVisible()
}
}

当应用程序在后台时,一切正常,但当应用程序退出时,我收到远程通知,它只会启动应用程序。如果应用程序之前退出,是否有可以调用的方法?

最佳答案

当您的应用程序在退出应用程序后启动时,同时您收到远程通知,然后 didFinishLaunchingWithOptions 是 AppDelegate 中的第一个方法,它在启动应用程序时触发,检查您是否收到任何通知通知与否并相应地执行您的操作。

您必须在应用委托(delegate)中寻找此方法:-

-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

现在检查您的应用是否收到任何推送通知

UILocalNotification *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (notification) {
NSLog(@"app received a notification %@",notification);
[self application:application didReceiveRemoteNotification:(NSDictionary*)notification];
}else{
NSLog(@"app did not receive a notification");
}

关于ios - 退出应用程序时从远程通知显示 uiviewcontroller,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30375391/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com