Android - InstanceID.GetToken抛出Java.Lang.IncompatibleClassChangeError

时间:2016-11-03 14:56:56

标签: c# android xamarin visual-studio-2015 token

我目前正在向我的Android应用添加通知。我正在使用VS2015和Xamarin。我想我会先创建一个侧面项目来测试它。我使用了Xamarin和Firebase的文档。

当我的应用启动时,它会尝试创建一个新令牌,但我收到了错误:

  

Java.Lang.IncompatibleClassChangeError:方法' java.io.File   android.support.v4.content.ContextCompat.getNoBackupFilesDir(android.content.Context)'   预计是虚拟类型,但发现是类型   直接

我提取令牌的代码是

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看起来像这样

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

我的所有套餐都是最新的并使用最新的稳定版:

  • Xamarin.GooglePlayServices.Gcm版本9.0.0.2及其所有依赖项
  • Xamarin.Android.Support.v4 Version 24.2.1

0 个答案:

没有答案