如果应用程序处于非活动状态,则访

时间:2016-01-21 12:09:57

标签: ios push appdelegate

我有推送通知,当应用收到时,我会调用以下内容

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

    }

}

这个初始化故事板。但是如果我的应用程序被系统杀死并且它关闭并且设备接收推送,则在点击推送后没有任何事情发生。

如果应用已关闭,我似乎必须使用application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?)

但是如何在didFinishLaunchingWithOptions中访问userInfo?

2 个答案:

答案 0 :(得分:1)

这是在Objective C中,但在Swift中也是如此。把它放在didFinishLaunchingWithOptions

NSDictionary *remoteNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (remoteNotif) {
    [self application:application didReceiveRemoteNotification:remoteNotif];
}

夫特:

if let remoteNotif = launchOptions?[UIApplicationLaunchOptionsRemoteNotificationKey] as? NSDictionary {...}

答案 1 :(得分:1)

您可以使用UIApplicationLaunchOptionsRemoteNotificationKey作为启动选项在didFinishLaunching中查看此内容。

  

UIApplicationLaunchOptionsRemoteNotificationKey:表示a   远程通知可供应用程序处理。的价值   此键是包含远程有效负载的NSDictionary   通知。 > - alert:警报消息的字符串或a   带有两个键的字典:正文和展示视图。 > - 徽章:一个数字   指示要从提供程序下载的数据项的数量。   此号码将显示在应用程序图标上。没有徽章   property表示当前标记图标的任何数字都应该   被删除。 > - 声音:应用程序包中声音文件的名称   播放作为警报声。如果指定“default”,则为默认声音   应该玩。

您可以手动拨打application:didReceiveRemoteNotification:中的application:didFinishLaunchingWithOptions:

目标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
    }