当应用程序未处于 运行 状态时,如何在应用程序启动时读取 aps 负载

How to read aps payload on app launch when app was not in running state

如何在收到推送通知时读取 aps 负载并且应用程序正在从非 运行 状态启动。 这是我试过的。 在 side app delegate class 中,在函数

FinishedLaunching (UIApplication app, NSDictionary options)
    if (options != null){
     if (options.ContainsKey(UIApplication.LaunchOptionsRemoteNotificationKey)){
       NSDictionary userInfo =(NSDictionary)options[UIApplication.LaunchOptionsRemoteNotificationKey];
       if (userInfo != null){
         if (null != options && options.ContainsKey(new NSString("aps")))
                {
                    NSDictionary aps = options.ObjectForKey(new NSString("aps")) as NSDictionary;
           if (aps.ContainsKey(new NSString("title")))
                        title = (aps[new NSString("title")] as NSString).ToString();
                 }
        }
    }
}

但是我无法读取数据。但是如果应用程序处于 运行 状态(active/inactive),我可以从方法

中以这种方式读取
ReceivedRemoteNotification(UIApplication application, NSDictionary userInfo)

在您的 application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

中使用它
//---------------------------------------------------
    //Handel Push notification
    if (launchOptions != nil)
    {
        // Here app will open from pushnotification
        //RemoteNotification
        NSDictionary* dictionary1 = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
        //LocalNotification
        NSDictionary* dictionary2 = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
        if (dictionary1 != nil)
        {
            //RemoteNotification Payload
            NSLog(@"Launched from push notification: %@", dictionary1);
            // here set you code
        }
        if (dictionary2 != nil)
        {
            NSLog(@"Launched from dictionary2dictionary2dictionary2 notification: %@", dictionary2);
            double delayInSeconds = 7;
            dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
            dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
                // [self addMessageFromRemoteNotification:dictionary2 updateUI:NO];
            });
        }

     }
     else
         {}

找到正确的方法了。 options 没有直接包含数据,而是必须检查 options

内的 userinfo
FinishedLaunching (UIApplication app, NSDictionary options)
    if (options != null){
     if (options.ContainsKey(UIApplication.LaunchOptionsRemoteNotificationKey)){
       NSDictionary userInfo =(NSDictionary)options[UIApplication.LaunchOptionsRemoteNotificationKey];
       if (userInfo != null){
         if (null != userInfo && userInfo .ContainsKey(new NSString("aps")))
                {
                    NSDictionary aps = userInfo .ObjectForKey(new NSString("aps")) as NSDictionary;
           if (aps.ContainsKey(new NSString("title")))
                        title = (aps[new NSString("title")] as NSString).ToString();
                 }
        }
    }