2

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:

var gcm = GoogleCloudMessaging.GetInstance(this);
var key = gcm.Register(new[] { "senderId" });

and it doesn't work, I fount that I need to run it on async thread, one solution I found here: http://forums.xamarin.com/discussion/8420/error-calling-googlecloudmessaging-register

ThreadPool.QueueUserWorkItem(o =>
{
    var gcm = GoogleCloudMessaging.GetInstance(this);
    var key = gcm.Register(new[] { Settings.GmcSenderId });
});

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:

var task = Task.Factory.Startnew(() =>
{
    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;

But every time I receive SERVICE_NOT_AVAILABLE Exception, also I have try to sync with ManualResetEvent object, but still have exceptions.

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...

My code with Task:

public string Register(string senderId)
{
    var task = Task.Factory.StartNew(() =>
    {
        var context = Mvx.Resolve<IMvxAndroidCurrentTopActivity>().Activity;
        var gcm = GoogleCloudMessaging.GetInstance(context);
        return gcm.Register(senderId);
    });
    return task.Result; // exception here!
}

Detailed exception:

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 <filename unknown>:0 
at Fiocx.Android.Code.NotificationService+<>c__DisplayClass1.<Register>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 <filename unknown>:0 
at System.Threading.Tasks.Task.InnerInvoke () [0x00000] in <filename unknown>:0 
at System.Threading.Tasks.Task.ThreadStart () [0x00000] in <filename unknown>: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

1 Answers1

0

Similar problem with a solution: GCM SERVICE_NOT_AVAILABLE on Android 2.2

Btw have you tried using PushSharp? https://github.com/Redth/PushSharp

Community
  • 1
  • 1
Adam Ivancza
  • 2,459
  • 1
  • 25
  • 36
  • PushSharp - server side library. But I am trying to get GCM device Registration ID on client, and than I'll send it to my server and use in PushSharp :) – bogdan.sachkovskyi Nov 19 '13 at 11:21
  • No, PushSharp has server and client side as well. Check out this sample: https://github.com/Redth/PushSharp/tree/master/PushSharp.Android/Gcm – Adam Ivancza Nov 19 '13 at 11:23
  • I don't know before about client side code. Thank you! I'll try it today, and post my results. – bogdan.sachkovskyi Nov 19 '13 at 11:41
  • I have tried PushSharp and see that it has internal realization like here http://docs.xamarin.com/guides/cross-platform/application_fundamentals/notifications/android/remote_notifications_in_android/ with StartService action. So yes it works, but I still can't do registration in synchronous way. My main idea was to not send any POST to server from Android code (push service) but as I see everybody do that, so I will also do. – bogdan.sachkovskyi Nov 20 '13 at 10:12