2016-08-11 101 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載荷? –

相關問題