2016-08-11 102 views
1

如何在接收到推送通知并从非运行状态启动应用程序时读取aps有效内容。 这是我试过的。 在端应用程序委托类,在一侧的功能如何在应用程序未启动时读取应用程序启动时的aps有效载荷

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(); 
       } 
     } 
    } 
} 

但我无法读取数据。但是,如果应用程序处于运行状态(启用/禁用),我能够从方法

ReceivedRemoteNotification(UIApplication application, NSDictionary userInfo) 

回答

0

发现了正确的方法用这种方式来阅读。选项犯规直接拥有的数据,而不是曾在用户信息中要检查里面的选项

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(); 
       } 
     } 
    } 
0

使用此在您的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 
     {} 
+0

我能够检测启动是否从推发生或不会形成LaunchOptionsRemoteNotificationKey。但无法从apns载荷读取任何数据 – TutuGeorge

+0

您能否在此处粘贴您的apns载荷? –

相关问题