Firebase 云消息推送在 Android 构建变体中无法工作

9
我在Firebase中有两个项目:nl.companynamenl.companyname.acc

enter image description here

这是我的build.gradle文件:
flavorDimensions "type"
productFlavors {
    acceptance {
        dimension="type"
        applicationIdSuffix ".acc"
        versionNameSuffix "-acc"
    }
    production {
        dimension="type"
        applicationIdSuffix ""
        versionNameSuffix ""
    }
}

下载 google-services.json 文件的目录为:app/google-services.json。
Android Studio 已登录 Google 账户并同步了:
点击可查看图片:enter image description here 发送消息成功,显示如下:
点击可查看图片:enter image description here 问题描述:
  • 在 nl.companyname 上发送消息可以成功。
  • 在以设备的 Token ID 作为目标时,消息也可以成功发送。
  • 但是使用 nl.companyname.acc 时无法发送消息。
已尝试的步骤:
  • 我已删除 Firebase 中的 .acc 应用并重新添加它(并下载了新的 json 文件)。
点击可查看图片:enter image description here 如有帮助将不胜感激。

你需要改变将google-services.json文件添加到应用程序文件夹的方法,有替代方案可供使用。 - Kintan Patel
3个回答

8

Firebase文档中,它支持基于多种口味的项目。

您可以通过在应用程序模块根目录下专门命名为每个变体的目录中放置google-services.json文件(针对不同构建变体)来拥有多个google-services.json文件。例如,如果您有“开发版”和“发布版”构建口味,则可以按如下方式组织配置:

app/
    google-services.json
    src/development/google-services.json
    src/release/google-services.json
    ...

您可以在这里找到完整的说明。

0
你确定你的google-services.json文件是这样的吗?
{
  "project_info": {
    ...
  },
  "client": [
    {
      "client_info": {
        ...
        "android_client_info": {
          "package_name": "nl.companyname"
        }
      },
      ...
    },
    {
      "client_info": {
        ...
        "android_client_info": {
          "package_name": "nl.companyname.acc"
        }
      },
      ...
    }
  ],
  ...
}

在你的 app/gralde.build 文件中尝试像这样的代码
buildTypes {
    acceptance {
        applicationIdSuffix '.acc'
        ...
    }
    release {
        ...
    }
}

0

从build.gradle中删除google-services.json

创建Application类

public class MyApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();

        FirebaseOptions options = new FirebaseOptions.Builder()
                .setApplicationId("0")
                .build();
        FirebaseApp.initializeApp(this, options);
    }

}

在清单文件中注册此类

 <application
        android:name=".extra.MyApplication"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:theme="@style/AppTheme">

在您的主活动中,您需要调用下面的方法来生成 FCM 令牌(类似于 FirebaseInstanceIdService)。
new GetFCMToken().execute();

 private class GetFCMToken extends AsyncTask<String,Integer,String>{

        @Override
        protected String doInBackground(String... params) {
            //you can update this id based on your flavour
        String senderId = "1223";//put your FireBase project Sender Id


            String sToken = "";
            try {
                // Check for current token

                // Resets Instance ID and revokes all tokens.
                FirebaseInstanceId.getInstance().deleteInstanceId();

                // Clear current saved token
                // Check for success of empty token


                // Now manually call onTokenRefresh()
                Log.d("TAG", "Getting new token");
                sToken = FirebaseInstanceId.getInstance().getToken(senderId, "FCM");
                Log.d("TAG", "s" + sToken);

            } catch (IOException e) {
                e.printStackTrace();
                Log.e("e", "e", e);
            }
            return sToken;
        }

        @Override
        protected void onPostExecute(String fcmToken) {
            super.onPostExecute(fcmToken);
            //Use this token to send notification
            Log.e("FCM_TOKEN",fcmToken);
           //Send Token server


        }

    }

您可以从Firebase控制台找到SenderId-->项目设置-->云消息传递。并确保已将您的包名添加到Fcm控制台中。

FirebaseMessegingReceiver.class会按原样工作。

此代码的主要好处包括:

  1. 无需添加google-services.json文件
  2. 单个代码可与多个Firebase项目一起使用
  3. 无需在清单中添加FirebaseInstanceIdService

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接