无法使用Unity的原生Android插件(FCM)

时间:2018-04-03 04:45:24

标签: android firebase unity3d firebase-cloud-messaging

错误

  • 安装unity apk文件,创建两个图标。(一个是调用Android onCreate函数,另一个是不调用)
  • Unity和FireBase相关的错误日志不可见,但它们没有收到推送通知。

错误日志

04-04 15:51:23.256 1440-1949/? E/InputDispatcher: channel '9ad1a6f com.gcm.test/com.gcm.test.MainActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
04-04 15:51:31.231 13391-13406/com.gcm.test E/LocationManager: [LGNSS] Disable_PrivacyLocation_Information [tOperator : LGU ] , [privacy_check : NA]
04-04 15:52:34.836 1440-5372/? E/UserRestrictionsUtils: Unknown restriction: com.gcm.test
04-04 15:52:36.419 1440-2244/? E/OverlayManager: Cannot found overlays for target: com.gcm.test
04-04 15:52:36.623 11556-11556/? E/AppInfoBase: Exception when retrieving package:com.gcm.test
android.content.pm.PackageManager$NameNotFoundException: com.gcm.test
    at android.app.ApplicationPackageManager.getPackageInfoAsUser(ApplicationPackageManager.java:155)
    at android.app.ApplicationPackageManager.getPackageInfo(ApplicationPackageManager.java:140)
    at com.android.settings.applications.AppInfoBase.retrieveAppEntry(AppInfoBase.java:168)
    at com.android.settings.applications.InstalledAppDetails.refreshUi(InstalledAppDetails.java:600)
    at com.android.settings.applications.InstalledAppDetails.onActivityResult(InstalledAppDetails.java:563)
    at android.app.Activity.dispatchActivityResult(Activity.java:7031)
    at android.app.ActivityThread.deliverResults(ActivityThread.java:4129)
    at android.app.ActivityThread.handleSendResult(ActivityThread.java:4176)
    at android.app.ActivityThread.-wrap20(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1560)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6247)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:872)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762)
04-04 15:52:36.804 10511-10511/? E/PermissionPresenter: Error getting package:com.gcm.test
android.content.pm.PackageManager$NameNotFoundException: com.gcm.test
    at android.app.ApplicationPackageManager.getPackageInfoAsUser(ApplicationPackageManager.java:155)
    at android.app.ApplicationPackageManager.getPackageInfo(ApplicationPackageManager.java:140)
    at com.android.packageinstaller.permission.service.RuntimePermissionPresenterServiceImpl.onGetAppPermissions(RuntimePermissionPresenterServiceImpl.java:43)
    at android.permissionpresenterservice.RuntimePermissionPresenterService$MyHandler.handleMessage(RuntimePermissionPresenterService.java:120)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6247)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:872)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762)

错误日志(已解决)

Caused by: java.lang.NoClassDefFoundError: Failed resolution of: Lcom/google/firebase/iid/FirebaseInstanceId;
    at com.gcm.test.MainActivity.onCreate(MainActivity.java:16)
    at android.app.Activity.performCreate(Activity.java:6772)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2715)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2823)
    at android.app.ActivityThread.-wrap12(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1545)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6349)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:893)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:783)
 Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.firebase.iid.FirebaseInstanceId" on path: DexPathList[[zip file "/data/app/com.gcm.test-1/base.apk"],nativeLibraryDirectories=[/data/app/com.gcm.test-1/lib/arm, /data/app/com.gcm.test-1/base.apk!/lib/armeabi-v7a, /system/lib, /vendor/lib]]
    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:380)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
    at com.gcm.test.MainActivity.onCreate(MainActivity.java:16) 
    at android.app.Activity.performCreate(Activity.java:6772) 
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119) 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2715) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2823) 
    at android.app.ActivityThread.-wrap12(ActivityThread.java) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1545) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:154) 
    at android.app.ActivityThread.main(ActivityThread.java:6349) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:893) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:783) 

通过将PlayerServices和Firebase插件文件放在Unity的插件文件夹中来解决此错误。

Unity代码

public class TestCode : MonoBehaviour {
AndroidJavaClass andClass;
AndroidJavaObject andObj;
void Start () {
        andClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
        andObj = andClass.GetStatic<AndroidJavaObject>("currentActivity");
}
}

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.gcm.test">

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />"

<application
    android:allowBackup="true"
    android:label="@string/app_name"
    android:supportsRtl="true">
    <activity android:name="com.gcm.test.MainActivity"
        android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize|mcc|mnc">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <service
        android:name="com.gcm.test.MyFirebaseMessagingService">
        <intent-filter android:priority="1000">
            <action android:name="com.google.firebase.MESSAGING_EVENT"/>
        </intent-filter>
    </service>
    <service
        android:name="com.gcm.test.MyFirebaseInstanceIDService">
        <intent-filter>
            <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
        </intent-filter>
    </service>
</application>
</manifest>

build.grade(模件:应用)

apply plugin: 'com.android.library'
android {
compileSdkVersion 25
defaultConfig {
    minSdkVersion 16
    targetSdkVersion 25
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
}

dependencies {
    compileOnly fileTree(include: ['*.jar'], dir: 'libs')
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
    implementation 'com.google.firebase:firebase-core:11.8.0'
    implementation 'com.google.firebase:firebase-messaging:11.8.0'
    implementation 'com.google.firebase:firebase-iid:11.8.0'
    implementation 'com.google.firebase:firebase-common:11.8.0'
    implementation 'com.firebase:firebase-jobdispatcher:0.8.5'
    compileOnly files('libs/classes.jar')
    implementation 'com.android.support:support-v4:25.2.0'
    implementation 'com.android.support:appcompat-v7:25.2.0'
}
apply plugin: 'com.google.gms.google-services'

MainActivity.java

public class MainActivity extends UnityPlayerActivity {
private static MainActivity instance = null;

public static MainActivity getInstance() {
    if(instance == null) {
        instance = new MainActivity();
    }
    return instance;
}

@Override
protected void onCreate(Bundle bundle) {
    super.onCreate(bundle);
    FirebaseApp.initializeApp(this);
    Log.d("unity", "StartActivity");
}
}

另一个代码看起来就像这段代码。

https://github.com/firebase/quickstart-android/tree/master/messaging/app/src/main/java/com/google/firebase/quickstart/fcm

0 个答案:

没有答案