title: GCM xamarin: Register
body: <p>I am using xamarin android 4.10.1 and GooglePlayServices Rev. 12 from xamarin component store. It is mvvmcross application, so I have Core and Android projects. I need push notifications support in my app. So I start with GooglePlayServices component. I write this code:</p>
  
  <pre><code>var gcm = GoogleCloudMessaging.GetInstance(this);
  var key = gcm.Register(new[] { "senderId" });
  </code></pre>
  
  <p>and it doesn't work, I fount that I need to run it on async thread, one solution I found here: <a href="http://forums.xamarin.com/discussion/8420/error-calling-googlecloudmessaging-register" rel="nofollow">http://forums.xamarin.com/discussion/8420/error-calling-googlecloudmessaging-register</a></p>
  
  <pre><code>ThreadPool.QueueUserWorkItem(o =&gt;
  {
      var gcm = GoogleCloudMessaging.GetInstance(this);
      var key = gcm.Register(new[] { Settings.GmcSenderId });
  });
  </code></pre>
  
  <p>This code works, my service handle registration message, but I need this registration key in my mvvmcross ViewModel. So I start to register with Task approach:</p>
  
  <pre><code>var task = Task.Factory.Startnew(() =&gt;
  {
      var gcm = GoogleCloudMessaging.GetInstance(this);
      return gcm.Register(new[] { Settings.GmcSenderId });
  });
  var key = task.Result; // wait for result
  // key is needed to execute code here
  // ViewModel.Key = key;
  </code></pre>
  
  <p>But every time I receive SERVICE_NOT_AVAILABLE Exception, also I have try to sync with ManualResetEvent object, but still have exceptions.</p>
  
  <p>Maybe some one know solution, how to bring registration Id to ViewModel class from View (activity). Or maybe you have some example with mvvmcross and receiving registration Id in view model...</p>
  
  <p>My code with Task:</p>
  
  <pre><code>public string Register(string senderId)
  {
      var task = Task.Factory.StartNew(() =&gt;
      {
          var context = Mvx.Resolve&lt;IMvxAndroidCurrentTopActivity&gt;().Activity;
          var gcm = GoogleCloudMessaging.GetInstance(context);
          return gcm.Register(senderId);
      });
      return task.Result; // exception here!
  }
  </code></pre>
  
  <p>Detailed exception:</p>
  
  <pre><code>InnerException  {Java.IO.IOException: Exception of type 'Java.IO.IOException' was thrown.
  at Android.Runtime.JNIEnv.CallObjectMethod (IntPtr jobject, IntPtr jmethod, Android.Runtime.JValue[] parms) [0x00064] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.10.1-branch/d23a19bf/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.g.cs:194 
  at Android.Gms.Gcm.GoogleCloudMessaging.Register (System.String[] p0) [0x00000] in &lt;filename unknown&gt;:0 
  at Fiocx.Android.Code.NotificationService+&lt;&gt;c__DisplayClass1.&lt;Register&gt;b__0 () [0x00013] in d:\ASUS\Work\Programming\.NET\Fiocx.CloudApp\mobile_src\Fiocx.Mobile\Fiocx.Android\Code\NotificationService.cs:33 
  at System.Threading.Tasks.TaskActionInvoker+FuncInvoke`1[System.String].Invoke (System.Threading.Tasks.Task owner, System.Object state, System.Threading.Tasks.Task context) [0x00000] in &lt;filename unknown&gt;:0 
  at System.Threading.Tasks.Task.InnerInvoke () [0x00000] in &lt;filename unknown&gt;:0 
  at System.Threading.Tasks.Task.ThreadStart () [0x00000] in &lt;filename unknown&gt;:0 
  --- End of managed exception stack trace ---
  java.io.IOException: SERVICE_NOT_AVAILABLE
  at com.google.android.gms.gcm.GoogleCloudMessaging.register(Unknown Source)
  at dalvik.system.NativeStart.run(Native Method)
  }   Java.IO.IOException
  </code></pre>