FCM - 在服务中找不到Xamarin类

时间:2017-10-02 01:10:52

标签: android firebase xamarin xamarin.android firebase-cloud-messaging

我遇到FCM问题,该应用无法找到firebaseInstanceIdService和firebaseMessagingService,更多详情,这是例外:

    [AndroidRuntime] FATAL EXCEPTION: main
    [AndroidRuntime] Process: com.tracker.test, PID: 13287
    [AndroidRuntime] java.lang.RuntimeException: Unable to instantiate service com.tracker.test.MyFirebaseInstanceIdService: java.lang.ClassNotFoundException: Didn't find class "com.tracker.test.MyFirebaseInstanceIdService" on path: DexPathList[[zip file "/data/app/com.tracker.test-1/base.apk"],nativeLibraryDirectories=[/data/app/com.tracker.test-1/lib/arm, /system/fake-libs, /data/app/com.tracker.test-1/base.apk!/lib/armeabi-v7a, /system/lib, /vendor/lib, /system/vendor/lib, /product/lib]]
    [AndroidRuntime]    at android.app.ActivityThread.handleCreateService(ActivityThread.java:3313)
    [AndroidRuntime]    at android.app.ActivityThread.-wrap5(ActivityThread.java)
    [AndroidRuntime]    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1659)
    [AndroidRuntime]    at android.os.Handler.dispatchMessage(Handler.java:105)
    [AndroidRuntime]    at android.os.Looper.loop(Looper.java:156)
    [AndroidRuntime]    at android.app.ActivityThread.main(ActivityThread.java:6524)
    [AndroidRuntime]    at java.lang.reflect.Method.invoke(Native Method)
    [AndroidRuntime]    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:941)
    [AndroidRuntime]    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:831)
    [AndroidRuntime] Caused by: java.lang.ClassNotFoundException: Didn't find class "com.tracker.test.MyFirebaseInstanceIdService" on path: DexPathList[[zip file "/data/app/com.tracker.test-1/base.apk"],nativeLibraryDirectories=[/data/app/com.tracker.test-1/lib/arm, /system/fake-libs, /data/app/com.tracker.test-1/base.apk!/lib/armeabi-v7a, /system/lib, /vendor/lib, /system/vendor/lib, /product/lib]]
    [AndroidRuntime]    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
    [AndroidRuntime]    at java.lang.ClassLoader.loadClass(ClassLoader.java:380)
    [AndroidRuntime]    at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
    [AndroidRuntime]    at android.app.ActivityThread.handleCreateService(ActivityThread.java:3310)
    [AndroidRuntime]    ... 8 more
    [Process] Sending signal. PID: 13287 SIG: 9

AndroidManifest看起来像这样:

    <?xml version="1.0" encoding="utf-8"?>

    <manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.tracker.test">
<uses-sdk android:minSdkVersion="15" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<application android:label="test">
    <service android:name="MyFirebaseInstanceIdService" android:exported="true">
        <intent-filter>
            <action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
        </intent-filter>
    </service>
    <service android:name="MyFirebaseMessagingService" android:exported="true">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>
    </service>
    <receiver android:name="com.google.firebase.iid.FirebaseInstanceIdInternalReceiver" android:exported="false" />
    <receiver android:name="com.google.firebase.iid.FirebaseInstanceIdReceiver" 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="${applicationId}" />
        </intent-filter>
    </receiver>
</application>

    </manifest>

我尝试添加“。”在MyFirebaseInstanceIdService和MyFirebaseMessagingService之前。

using Android.App;
using Android.Content;
using Android.Media;
using Android.Support.V4.App;
using Firebase.Messaging;

namespace test2.Droid
{
    [Service]
    [IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]
public class MyFirebaseMessagingService : FirebaseMessagingService
{
    const string TAG = "MyFirebaseMessagingService";

    public override void OnMessageReceived(RemoteMessage message)
    {
        Android.Util.Log.Debug(TAG, "From: " + message.From);
        Android.Util.Log.Debug(TAG, "Notification Message Body: " + message.GetNotification().Body);
    }

    void SendNotification(string messageBody)
    {
        var intent = new Intent(this, typeof(MainActivity));
        intent.AddFlags(ActivityFlags.ClearTop);
        var pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.OneShot);

        var defaultSoundUri = RingtoneManager.GetDefaultUri(RingtoneType.Notification);
        var notificationBuilder = new NotificationCompat.Builder(this)
            //.SetSmallIcon(Resource.Drawable.ic_stat_ic_notification)
            .SetContentTitle("FCM Message")
            .SetContentText(messageBody)
            .SetAutoCancel(true)
            .SetSound(defaultSoundUri)
            .SetContentIntent(pendingIntent);

        var notificationManager = NotificationManager.FromContext(this);

        notificationManager.Notify(0, notificationBuilder.Build());
    }
}

}

和MyFirebaseInstanceIdService

namespace test2.Droid
{
[Service]
[IntentFilter(new[] { "com.google.firebase.INSTANCE_ID_EVENT" })]
public class MyFirebaseInstanceIdService : FirebaseInstanceIdService
{
    const string TAG = "MyFirebaseInstanceIdService";
    public override void OnTokenRefresh()
    {
        var refreshedToken = FirebaseInstanceId.Instance.Token;
        Log.Debug(TAG, "Refreshed token: " + refreshedToken);
    }
}

任何帮助?

谢谢!

2 个答案:

答案 0 :(得分:0)

对于您的FirebaseMessagingService子类,添加类属性ServiceIntentFilter

示例:

[Service]
[IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]
public class MyFirebaseMessagingService : FirebaseMessagingService
{
  ~~~~
}

对于FirebaseInstanceIdService子类,添加相同但使用不同的intent过滤器:

[Service]
[IntentFilter(new[] { "com.google.firebase.INSTANCE_ID_EVENT" })]
public class MyFirebaseInstanceIdService : FirebaseInstanceIdService
{
  ~~~
}

在您的清单中,在您的应用标记中添加 中的Firebase接收器:

<application ...... >
    <receiver android:name="com.google.firebase.iid.FirebaseInstanceIdInternalReceiver" android:exported="false" />
    <receiver android:name="com.google.firebase.iid.FirebaseInstanceIdReceiver" 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="${applicationId}" />
        </intent-filter>
    </receiver>
</applcication>

答案 1 :(得分:0)

我遇到了同样的问题,并通过启用multidex,

来解决
  1. 在app gradle文件中添加以下依赖项 compile 'com.android.support:multidex:1.0.1'
  2. defaultConfig {
     multiDexEnabled true
    }
    
    1. 您的应用类需要扩展MultiDexApplication