2016-05-23 15 views
0

iOSアプリケーションのリモート通知を実装しています。アプリが実行されていないときに通知ペイロードをキャッチしようとしています。問題は、FinishedLaunchingが呼び出されると、launchOptionパラメータは常にnullです。他のケースでは、アプリがバックグラウンドまたはアクティブになっているときに通知が正常に機能します。 私は参照やブログの投稿に基づいて、それが機能するはずの理由を理解していません。これは私が使用しているコードです:FinishedLaunchingのLaunchOptionは常にnullです(Xamarin.iOS)

public override bool FinishedLaunching (UIApplication application, NSDictionary launchOptions) 
    { 
      //new UIAlertView("launchOption", launchOptions != null ? "Yes" : "No", null, "").Show(); 
      if(launchOptions != null && launchOptions.ContainsKey(UIApplication.LaunchOptionsRemoteNotificationKey)) 
      { 
       //new UIAlertView("launchOption", "launchOption != null", null, ""); 
       this.viewModel.Loading(false); 

       NSObject result; 
       if (launchOptions.TryGetValue (UIApplication.LaunchOptionsRemoteNotificationKey, out result)){ 
        NSNotification notification = result as NSNotification; 
        DispatchPushNotification(application, notification.UserInfo); 
        Console.WriteLine ("Got a local notification: {0}", notification); 
       } 
      ... 

私は間違っていますか?

+0

これについては解決策が見つかりましたか?、私も同様の問題に直面しています – Subha

答えて

0

私は正確に問題がありました、ペイロードはnullとして来ていました。問題は、次のように完成した起動オプションをタイプキャストしていないことです。

NSDictionary userInfo = (NSDictionary)options [UIApplication.LaunchOptionsRemoteNotificationKey]; 

タイプキャスト後、私は適切なペイロードを取得し始めました。 launchOptionsに直接アクセスしているようです。

1

ペイロードを取得するコードを変更して解決しました。私は、options.TryGetValue(...)を使用するたびに起動オプションの入力値がnullであることに気付きました。この行を削除し、以下を使用して私の問題を解決しました。

if (launchOptions != null && launchOptions.ContainsKey (UIApplication.LaunchOptionsRemoteNotificationKey)) { 
    Console.WriteLine ("launchOption != null and LaunchOptionsRemoteNotificationKey present"); 
    NSDictionary notification = launchOptions.ObjectForKey(UIApplication.LaunchOptionsRemoteNotificationKey) as NSDictionary; 
    if (notification != null) 
     // do something 
    } 
} 

バグかどうかわかりませんが、テストするにはあまり時間がかかりませんでした。

関連する問題