辅助应用程序的 FirebaseMessaging.getInstance(firebaseApp) 应该是公开的,但它是私有的? [英] FirebaseMessaging.getInstance(firebaseApp) for secondary app supposed to be public but it's private?

查看:17
本文介绍了辅助应用程序的 FirebaseMessaging.getInstance(firebaseApp) 应该是公开的,但它是私有的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

公共静态同步 FirebaseMessaging getInstance(FirebaseApp 应用)

<块引用>

获取指定 FirebaseApp 的 FirebaseMessaging 实例.

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

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

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

public class FirebaseMessaging {public static final String INSTANCE_ID_SCOPE = "FCM";私有最终上下文zzb;私有最终 FirebaseInstanceId zzc;私人最终任务<zzab>zd;@Nullable@SuppressLint({"FirebaseUnknownNullness"})@VisibleForTesting静态传输工厂 zza;@非空公共静态同步 FirebaseMessaging getInstance() {返回 getInstance(FirebaseApp.getInstance());}@保持@非空静态同步 FirebaseMessaging getInstance(@NonNull FirebaseApp var0) {返回(FirebaseMessaging)var0.get(FirebaseMessaging.class);}

我错过了什么吗?

解决方案

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

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

现在根据 文档 :

<块引用>

public void onNewToken (String token)

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

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

因此,在这种情况下,使用 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 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,获取 FCM 令牌失败", it.exception)返回@addOnCompleteListener}val 令牌 = 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.

KEY CODE

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) 应该是公开的,但它是私有的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆