要在没有Firebase Cloud Messaging的情况下在Unity中获得当前 Activity ,可以使用以下代码:
var unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
var activity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
但是,Firebase Cloud Messaging扩展了默认的Unity Activity 。因此,我将AndroidJavaClass
更改为:var unityPlayer = new AndroidJavaClass("com.google.firebase.MessagingUnityPlayerActivity");
var activity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
但是,我现在在logcat中收到以下错误:08-01 15:51:31.838 20323 20375 E Unity : AndroidJavaException: java.lang.NoSuchFieldError: no "Ljava/lang/Object;" field "currentActivity" in class "Lcom/google/firebase/MessagingUnityPlayerActivity;" or its superclasses
08-01 15:51:31.838 20323 20375 E Unity : java.lang.NoSuchFieldError: no "Ljava/lang/Object;" field "currentActivity" in class "Lcom/google/firebase/MessagingUnityPlayerActivity;" or its superclasses
08-01 15:51:31.838 20323 20375 E Unity : at com.unity3d.player.UnityPlayer.nativeRender(Native Method)
08-01 15:51:31.838 20323 20375 E Unity : at com.unity3d.player.UnityPlayer.access$300(Unknown Source:0)
08-01 15:51:31.838 20323 20375 E Unity : at com.unity3d.player.UnityPlayer$e$1.handleMessage(Unknown Source:83)
08-01 15:51:31.838 20323 20375 E Unity : at android.os.Handler.dispatchMessage(Handler.java:103)
08-01 15:51:31.838 20323 20375 E Unity : at android.os.Looper.loop(Looper.java:214)
08-01 15:51:31.838 20323 20375 E Unity : at com.unity3d.player.UnityPlayer$e.run(Unknown Source:20)
08-01 15:51:31.838 20323 20375 E Unity : at UnityEngine.AndroidJNISafe.CheckException () [0x00000] in <00000000000000000000000000000000>:0
08-01 15:51:31.838 20323 20375 E Unity : at UnityEngine.AndroidJNISafe.GetStaticFieldID (System.IntPtr clazz, System.String name, System.String sig) [0x00000] in <00000000000000000000000000000000>:0
08-01 15:51:31.838 20323 20375 E Unity : at UnityEngine._AndroidJNIHelper.GetFieldID (System.IntPtr jc
我不明白为什么。它说currentActivity
甚至不存在于父类(super class)中,但是如果我正确理解Firebase Cloud Messaging,则它应该仍然存在于父类(super class)中。我也尝试过用currentActivity
,UnityPlayerActivity
,MessagingUnityPlayerActivity
和mypackage
之类的东西替换com.mypackage
,但是存在相同的错误。在使用Firebase Cloud Messaging时在我的AndroidJavaClass中引用com.unity3d.player.UnityPlayer
有其自身的问题:https://gamedev.stackexchange.com/questions/183734/how-can-i-use-an-inherited-activity-in-c如果相关,这是我的
AndroidManifest.xml
:<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" package="com.mypackage" android:versionCode="1" android:versionName="1.0">
<application android:label="@string/app_name" android:icon="@drawable/app_icon">
<!-- The MessagingUnityPlayerActivity is a class that extends
UnityPlayerActivity to work around a known issue when receiving
notification data payloads in the background. -->
<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="@drawable/notification_icon" />
<activity android:name="com.google.firebase.MessagingUnityPlayerActivity" android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data android:name="unityplayer.UnityActivity" android:value="true" />
</activity>
<service android:name="com.google.firebase.messaging.MessageForwardingService" android:exported="false" />
</application>
</manifest>
使用Firebase Cloud Messaging时如何获得当前 Activity ?为什么代码不能按所示方式工作? 最佳答案
如果您将类改回"com.unity3d.player.UnityPlayer"
,则此行为应符合预期。
问题在于,静态方法在Java中的行为与标准方法略有不同。如果看到Java documentation for the JNI。
请注意,这可能与您从标准Java文档(例如:Are static methods inherited in Java?)中得出的结论相反。