gpt4 book ai didi

c# - 在 Unity 中使用 Firebase 云消息传递时如何获取当前 Activity ?

转载 作者:行者123 更新时间:2023-12-03 09:26:48 26 4
gpt4 key购买 nike

要在不使用 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 - 但同样的错误。引用 com.unity3d.player.UnityPlayer在我的 AndroidJavaClass 中使用 Firebase 云消息传递有其自身的问题: 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 云消息传递时如何获取当前 Activity ?为什么代码不能如图所示工作?

最佳答案

如果您将类改回 "com.unity3d.player.UnityPlayer",这应该符合预期。 .
问题是静态方法在 Java 中的行为与标准方法略有不同。如果您看到 Java documentation for the JNI .

The method ID must be derived from clazz, not from one of its superclasses.


请注意,这与您从标准 Java 文档中推断的内容相反(例如: Are static methods inherited in Java?)。

关于c# - 在 Unity 中使用 Firebase 云消息传递时如何获取当前 Activity ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63210429/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com