问题描述
我有一个推送通知,当应用收到通知时,我会调用以下
I have a push notification, and when app receives it, I call the following
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
if userInfo["t"] as! String == "rqst" {
print("type is help request")
if let token = NSUserDefaults.standardUserDefaults().objectForKey("authToken") {
authTokenOfHelper = token as! String
}
let storyBoard = UIStoryboard.init(name: "Main", bundle: nil)
let viewController = storyBoard.instantiateViewControllerWithIdentifier("helperMap")
let navController = UINavigationController.init(rootViewController: viewController)
self.window?.rootViewController = nil
self.window?.rootViewController = navController
self.window?.makeKeyAndVisible()
helpRequestReceived = true
}
}
这会初始化情节提要.但是,如果我的应用程序被系统杀死并且关闭并且设备接收到推送,则在点击推送后,什么都不会发生.
this initialises storyboard.But if my app was killed by system and it is off and device recieves push, after tapping on push nothing is happened.
如果应用已关闭,我似乎必须使用application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?)
Seems that I have to use application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?)
if app is switched off
但是如何在didFinishLaunchingWithOptions中访问userInfo?
But how to access userInfo in didFinishLaunchingWithOptions ?
推荐答案
您可以使用UIApplicationLaunchOptionsRemoteNotificationKey
作为启动选项在didFinishLaunching中进行检查.
You can check this in didFinishLaunching using UIApplicationLaunchOptionsRemoteNotificationKey
as launch options.
您可以在application:didFinishLaunchingWithOptions:
中手动调用application:didReceiveRemoteNotification:
.
目标C
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// ...
if (launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey]) {
[self application:application didReceiveRemoteNotification:launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey]];
}
return YES;
}
快速
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
if let remoteNotification = launchOptions?[UIApplicationLaunchOptionsRemoteNotificationKey] as? NSDictionary {
self.application(application, didReceiveRemoteNotification: launchOptions![UIApplicationLaunchOptionsRemoteNotificationKey]! as! [NSObject : AnyObject])
}
return true
}
这篇关于如果应用程序处于非活动状态,则访问推送负载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!