辅助应用程序的 FirebaseMessaging.getInstance(firebaseApp) 应该是公共的,但它是私有的?

FirebaseMessaging.getInstance(firebaseApp) for secondary app supposed to be public but it#39;s private?(辅助应用程序的 FirebaseMessaging.getInstance(firebaseApp) 应该是公共的,但它是私有的?)
本文介绍了辅助应用程序的 FirebaseMessaging.getInstance(firebaseApp) 应该是公共的,但它是私有的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为辅助 Firebase 应用订阅 FCM(Firebase 云消息传递)主题,根据文档,这可以通过将辅助 FirebaseApp 实例作为重载的 getInstance 来完成一个参数:

https://firebase.google.com/docs/reference/admin/java/reference/com/google/firebase/messaging/FirebaseMessaging#public-static-synchronized-firebasemessaging-getinstance-firebaseapp-应用程序

public static synchronized FirebaseMessaging getInstance(FirebaseApp 应用)

<块引用>

获取指定 FirebaseApp 的 FirebaseMessaging 实例.

我正在使用 Kotlin,我正在像这样在 build.gradle 中拉入包:

实现com.google.firebase:firebase-messaging:20.2.0"

但是当我尝试使用重载的 getInstance 实例化 FirebaseMessaging 时,我收到一条错误消息,指出它不可访问.看包源码,反编译显示,重载的构造函数不像无参的getInstance那样是public的:

公共类 FirebaseMessaging {公共静态最终字符串 INSTANCE_ID_SCOPE = "FCM";私有最终上下文 zzb;私有最终 FirebaseInstanceId zzc;私人最终任务<zzab>zd;@Nullable@SuppressLint({"FirebaseUnknownNullness"})@VisibleForTesting静态 TransportFactory zza;@NonNull公共静态同步 FirebaseMessaging getInstance() {返回 getInstance(FirebaseApp.getInstance());}@保持@NonNull静态同步 FirebaseMessaging getInstance(@NonNull FirebaseApp var0) {返回 (FirebaseMessaging)var0.get(FirebaseMessaging.class);}

我错过了什么吗?

解决方案

我已经验证了一种方法,因为我遇到了类似的问题.

我使用 google-services.json 文件注册了其中一个项目.

现在根据 文档 :

<块引用>

public void onNewToken(字符串令牌)

在为 default Firebase 项目生成新令牌时调用.

这里是默认"这个词是至关重要的.它提到重写的 FirebaseMessagingService 中的 onNewToken 方法(例如:MyFirebaseMessagingService)只会为默认项目调用.

因此在这种情况下,使用 google-services.json 配置的第一个项目将是默认项目,并且将调用 onNewToken 方法.

对于第二个项目,我按照文档:

val options = FirebaseOptions.Builder().setProjectId("my-firebase-project").setApplicationId("1:27992087142:android:ce3b6448250083d1").setApiKey("AIzaSyADUe90ULnQDuGShD9W23RDP0xmeDc6Mvw").建造()

参数值可以从第二个项目的google-services.json文件中获取.(注意:不要在项目中包含第二个项目的 google-services.json)

google-services.json 到手动代码映射

  1. projectId (setProjectId) : project_id key 在 json 的根目录
  2. applicationid(setApplicationId): client>客户信息>mobilesdk_app_id.如果有多个项目,请确保使用的客户端是与 Android 应用匹配的 package_name
  3. apiKey (setApiKey) : client >api_key >current_key(这里也要确保包名.

密钥代码

在文档中很难找到的最重要部分是获取第二个 firebase 项目的令牌.

val app = Firebase.initialize(this, options, "ANY_FIXED_STRING_EXCEPT_DEFAULT")val firebaseMessaging = app.get(FirebaseMessaging::class.java) 作为 FirebaseMessagingymFirebaseMessaging.token.addOnCompleteListener{如果(!it.isSuccessful){Log.d(TAG, "Fetching FCM token failed", it.exception)返回@addOnCompleteListener}val token = it.resultLog.d(TAG, "YM: $token")Toast.makeText(活动,"$TAG: 得到令牌",吐司.LENGTH_LONG).展示()}

I'm trying to subscribe to an FCM (Firebase Cloud Messaging) topic for a secondary Firebase App and according to the documentation this could be done by the overloaded getInstance which takes the secondary FirebaseApp instance as a parameter:

https://firebase.google.com/docs/reference/admin/java/reference/com/google/firebase/messaging/FirebaseMessaging#public-static-synchronized-firebasemessaging-getinstance-firebaseapp-app

public static synchronized FirebaseMessaging getInstance (FirebaseApp app)

Gets the FirebaseMessaging instance for the specified FirebaseApp.

I'm using Kotlin and I'm pulling in the package in build.gradle like this:

implementation "com.google.firebase:firebase-messaging:20.2.0"

But when I try to instantiate the FirebaseMessaging with the overloaded getInstance, I get an error stating that it's not accessible. When I look at the package source, the decompilation shows that the overloaded constructor is not public like the parameterless getInstance:

public class FirebaseMessaging {
    public static final String INSTANCE_ID_SCOPE = "FCM";
    private final Context zzb;
    private final FirebaseInstanceId zzc;
    private final Task<zzab> zzd;
    @Nullable
    @SuppressLint({"FirebaseUnknownNullness"})
    @VisibleForTesting
    static TransportFactory zza;

    @NonNull
    public static synchronized FirebaseMessaging getInstance() {
        return getInstance(FirebaseApp.getInstance());
    }

    @Keep
    @NonNull
    static synchronized FirebaseMessaging getInstance(@NonNull FirebaseApp var0) {
        return (FirebaseMessaging)var0.get(FirebaseMessaging.class);
    }

Did I miss something?

解决方案

I have verified a way to do it as I had been facing a similar issue.

I registered one of the projects using the google-services.json file.

Now as per the documentation :

public void onNewToken (String token)

Called when a new token for the default Firebase project is generated.

Here the word "default" is of key importance. It mentions that the onNewToken method in the overridden FirebaseMessagingService (eg: MyFirebaseMessagingService) will only be called for the default project.

Hence in this case the first project configured using the google-services.json will be the default project and for that the onNewToken method will be called.

For the second project, I manually configured the project using the code below following the documentation:

val options = FirebaseOptions.Builder()
        .setProjectId("my-firebase-project")
        .setApplicationId("1:27992087142:android:ce3b6448250083d1")
        .setApiKey("AIzaSyADUe90ULnQDuGShD9W23RDP0xmeDc6Mvw")
        .build()

The values for the parameters can be obtained from the google-services.json file of the second project. (NOTE: DON'T INCLUDE THE SECOND PROJECT'S google-services.json IN THE PROJECT)

google-services.json to manual code mapping

  1. projectId (setProjectId) : project_id key in the root of the json
  2. applicationid (setApplicationId): client > client_info > mobilesdk_app_id. Incase of multiple project make sure that the client used is of the package_name which matches the Android app
  3. apiKey (setApiKey) : client > api_key > current_key (make sure of the package name here as well.

KEYCODE

The most important part here which is tough to find in the documentation is to get the token of the second firebase project.

val app = Firebase.initialize(this, options, "ANY_FIXED_STRING_EXCEPT_DEFAULT")

val firebaseMessaging = app.get(FirebaseMessaging::class.java) as FirebaseMessaging

ymFirebaseMessaging.token.addOnCompleteListener{
                if (!it.isSuccessful) {
                    Log.d(TAG, "Fetching FCM token failed", it.exception)

                    return@addOnCompleteListener
                }


                val token = it.result
                Log.d(TAG, "YM: $token")
                Toast.makeText(
                    activity,
                    "$TAG: Got token",
                    Toast.LENGTH_LONG
                ).show()
}

这篇关于辅助应用程序的 FirebaseMessaging.getInstance(firebaseApp) 应该是公共的,但它是私有的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)
Crash on Google Play Pre-Launch Report: java.lang.NoSuchMethodError(Google Play发布前崩溃报告:java.lang.NoSuchMethodError)