Ramote notefications example just stops?

Hello Guys!

I'm fairly new to Xamarin and coding Apps and I'm trying to make an app that uses Remote notifications by following this walkthrough:
https://developer.xamarin.com/guides/cross-platform/application_fundamentals/notifications/android/remote_notifications_in_android/

I followed the the guide and created an API Key and a Sender ID and then replaced the
"YOUR_SENDER_ID" with my own and then launched the app as instructed at the end
of the "Register with GCM"-step.

Everything works as expected up until the that "Register with GCM"-step.
After the app starts it displays "Google Play Services is available.", but then it just stops
and displays the message "ClientApp has been stopped", with no explanation.

After searching the internet for a while I found a possible solution and tried to "un check" the
"Use Shared Mono Runtime" and the "Fast Assembly Deployment" in the Android Build options
but that did nothing.

Has anyone else had this issue and if so how was it solved?
and / or does anyone have a working example where I could just replace the GCM-credentials
with my own etc?...

I'm running this on a Samsung Core (Samsung SM-G361F) using Android-version 5.1.1 (API 22)
And I'm using Xamarin Studio - Version 6.1 (build 5308).

Grateful for any help!!

/Mathias.A

Answers

  • Here is MainActivity:

    `using Android.App;
    using Android.Widget;
    using Android.OS;
    using Android.Gms.Common;
    using Android.Util;
    using Android.Content;

    namespace adaccuracy
    {
    [Activity(Label = "adaccuracy", MainLauncher = true, Icon = "@mipmap/icon")]
    public class MainActivity : Activity
    {
    TextView msgText;

        public bool IsPlayServicesAvailable()
        {
            int resultCode = GoogleApiAvailability.Instance.IsGooglePlayServicesAvailable(this);
            if (resultCode != ConnectionResult.Success)
            {
                if (GoogleApiAvailability.Instance.IsUserResolvableError(resultCode))
                    msgText.Text = GoogleApiAvailability.Instance.GetErrorString(resultCode);
                else
                {
                    msgText.Text = "Sorry, this device is not supported";
                    Finish();
                }
                return false;
            }
            else
            {
                msgText.Text = "Google Play Services is available.";
                return true;
            }
        }
    
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
    
            SetContentView(Resource.Layout.Main);
            msgText = FindViewById<TextView>(Resource.Id.msgText);
    
            if (IsPlayServicesAvailable())
            {
                var intent = new Intent(this, typeof(RegistrationIntentService));
                StartService(intent);
            }
        }
    }
    

    }

    `

    And here is RegistrationIntentService.se:

    `using System;
    using Android.App;
    using Android.Content;
    using Android.Util;
    using Android.Gms.Gcm;
    using Android.Gms.Gcm.Iid;

    namespace adaccuracy
    {
    [Service(Exported = false)]
    class RegistrationIntentService : IntentService
    {
    static object locker = new object();

        public RegistrationIntentService() : base("RegistrationIntentService") { }
    
        protected override void OnHandleIntent(Intent intent)
        {
            try
            {
                Log.Info("RegistrationIntentService", "Calling InstanceID.GetToken");
                lock (locker)
                {
                    var instanceID = InstanceID.GetInstance(this);
                    var token = instanceID.GetToken(
                        "585063357146", GoogleCloudMessaging.InstanceIdScope, null);
    
                    Log.Info("RegistrationIntentService", "GCM Registration Token: " + token);
                    SendRegistrationToAppServer(token);
                    Subscribe(token);
                }
            }
            catch (Exception e)
            {
                Log.Debug("RegistrationIntentService", "Failed to get a registration token");
                return;
            }
        }
    
        void SendRegistrationToAppServer(string token)
        {
            // Add custom implementation here as needed.
        }
    
        void Subscribe(string token)
        {
            var pubSub = GcmPubSub.GetInstance(this);
            pubSub.Subscribe(token, "/topics/global", null);
        }
    }
    

    }`

    Here is InstanceIdListenerService.se:

    `using Android.App;
    using Android.Content;
    using Android.Gms.Gcm.Iid;

    namespace adaccuracy
    {
    [Service(Exported = false), IntentFilter(new[] { "com.google.android.gms.iid.InstanceID" })]
    class MyInstanceIDListenerService : InstanceIDListenerService
    {
    public override void OnTokenRefresh()
    {
    var intent = new Intent(this, typeof(RegistrationIntentService));
    StartService(intent);
    }
    }
    }`

    Here is the AndroidManifest.xml:

    <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.adaccuracy.clientapp" android:versionCode="1" android:versionName="1.0" android:installLocation="auto"> <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /> <uses-permission android:name="android.permission.WAKE_LOCK" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="com.adaccuracy.clientapp.permission.C2D_MESSAGE" /> <permission android:name="com.adaccuracy.clientapp.permission.C2D_MESSAGE" android:protectionLevel="signature" /> <uses-sdk android:minSdkVersion="10" /> <application android:allowBackup="true" android:icon="@mipmap/icon" android:label="@string/app_name"></application> </manifest>

    And finally here is the AssemblyInfo.cs:

    `using System.Reflection;
    using System.Runtime.CompilerServices;
    using Android.App;

    // Information about this assembly is defined by the following attributes.
    // Change them to the values specific to your project.

    [assembly: AssemblyTitle("adaccuracy")]
    [assembly: AssemblyDescription("")]
    [assembly: AssemblyConfiguration("")]
    [assembly: AssemblyCompany("")]
    [assembly: AssemblyProduct("")]
    [assembly: AssemblyCopyright("${AuthorCopyright}")]
    [assembly: AssemblyTrademark("")]
    [assembly: AssemblyCulture("")]

    // The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}".
    // The form "{Major}.{Minor}." will automatically update the build and revision,
    // and "{Major}.{Minor}.{Build}.
    " will update just the revision.

    [assembly: AssemblyVersion("1.0.0")]

    // The following attributes are used to specify the signing key for the assembly,
    // if desired. See the Mono documentation for more information about signing.

    //[assembly: AssemblyDelaySign(false)]
    //[assembly: AssemblyKeyFile("")]
    `

    Now is there anything that I'm doing wrong that is obvious to someone else?
    I really need help guys because I have no clue what I'm doing wrong and I'm stuck :(

Sign In or Register to comment.