Firebase云消息传递不适用于Android上的Build变体

时间:2018-05-27 18:05:08

标签: android firebase firebase-cloud-messaging

我在Firebase中有2个项目: nl.companyname nl.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在目录中: 应用/谷歌services.json

Android Studio已登录Google帐户并已同步:

enter image description here

消息显示为已成功发送: enter image description here

问题描述:

  • nl.companyname 上发送消息时,它可以正常工作。

  • 发送邮件并定位设备的令牌ID 时,它可以正常工作。

  • nl.companyname.acc 无效。

尝试了一些步骤:

  • 我已删除Firebase中的.acc应用并重新添加(并下载了新的json文件)。

enter image description here

非常感谢任何帮助。

3 个答案:

答案 0 :(得分:6)

Firebase Document中,它支持多种基于风味的项目。

  

您可以为不同版本提供多个 google-services.json 文件   变种)将 google-services.json 文件放在专用的   为app模块根目录下的每个变体命名的目录。对于   例如,如果您有“开发”和“发布”构建风格,   配置可以像这样组织:

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

您可以在here找到完整说明。

答案 1 :(得分: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 {
        ...
    }
}

答案 2 :(得分:0)

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

创建应用程序类

public class MyApplication extends Application {

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

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

}

在Manifest中注册此类

 <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 cosole找到 SenderId - &gt;项目设置 - &gt;云消息传递。 并确保已在Fcm控制台中添加了包名称。

FirebaseMessegingReceiver.class 按原样运作。

此代码的主要好处是,

  1. 无需添加google-services.json
  2. 多个firebase项目使用单个代码。
  3. 无需在Manifest中添加FirebaseInstanceIdService。
相关问题