1

I am currently adding notifications to my Android app. I am using VS2015 and Xamarin. I figured I'd create a side project to test it first. I used the documentation from Xamarin and Firebase.

When my app launches, it tries to create a new token, but I get an error instead:

Java.Lang.IncompatibleClassChangeError: The method 'java.io.File android.support.v4.content.ContextCompat.getNoBackupFilesDir(android.content.Context)' was expected to be of type virtual but instead was found to be of type direct

My code for fetching the token is

MainActivity

        namespace ClientApp
        {
        [Activity(Label = "ClientApp", MainLauncher = true, Icon = "@drawable/icon")]
        public class MainActivity : Activity
        {

        TextView msgText;

        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(RegistrationIntentService3));
                StartService(intent);
            }
        }

        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;
            }
        }
    }
}

RegistrationIntentService.cs

using System;
using Android.App;
using Android.Content;
using Android.Util;
using Android.Gms.Gcm;
using Android.Gms.Gcm.Iid;
using Android.Support.V4.Content;



namespace ClientApp
{
    [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 token = InstanceID.GetInstance(this).GetToken("My_Sender_ID", 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);
        }
    }
}

My Manifest looks like this

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="MY_PACKAGE" 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="MY_PACKAGE.permission.C2D_MESSAGE" />
    <permission android:name="MY_PACKAGE.permission.C2D_MESSAGE" android:protectionLevel="signature" />
    <application android:label="RemoteNotifications" android:icon="@drawable/Icon">
        <receiver android:name="com.google.android.gms.gcm.GcmReceiver" android:exported="true" android:permission="com.google.android.c2dm.permission.SEND">
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
                <category android:name="MY_PACKAGE" />
            </intent-filter>
        </receiver>
    </application>
    <uses-sdk />
</manifest>

All my packages are up to date and using the latest stable:

  • Xamarin.GooglePlayServices.Gcm Version 9.0.0.2 with all its dependencies
  • Xamarin.Android.Support.v4 Version 24.2.1
ashley
  • 576
  • 3
  • 22
msin00
  • 19
  • 4
  • Have you migrated to Firebase Cloud Messaging ([new version of GCM](https://developers.google.com/cloud-messaging/))? "If you are integrating messaging in a new app, start with FCM. GCM users are strongly recommended to upgrade to FCM, in order to benefit from new FCM features today and in the future." It looks like a similar error was mentioned [here](http://stackoverflow.com/q/37312340/6146870). – ashley Nov 03 '16 at 21:28
  • I imported my app into FCM! I even tried changing the versions of my V4 packages as suggested in many threads i read online and still came up short. – msin00 Nov 03 '16 at 22:47
  • @ashley it seems that if i use the Android.Support.V4 Version 23.1.1.1, i am able to register a token with FCM. Unfortunately though, this will throw a lot of compatibility errors with other tools i am using most notably with the NagivationDrawer. I guess GooglePlayServices.Gcm Version 9.0.0.2 is currently only compatible with the Xamarin.Android.Support.v4 Version 23.1.1.1 and not the Xamarin.Android.Support.v4 Version 24.2.1. Should i consider this a bug? – msin00 Nov 07 '16 at 12:33
  • What are the specific errors? If they are about dependency conflicts/constraints [similar to this](http://stackoverflow.com/q/40384594/6146870), you may need to check the dependencies of the specific packages in the error messages to find the compatible version. – ashley Nov 08 '16 at 20:53

0 Answers0