更新:GCM已弃用,使用FCM

我正在实现新的谷歌云消息,遵循从谷歌开发人员页面这里的指南

我已经成功地运行并测试了它。但我现在的问题是,我有不同的产品口味,具有不同的applicationId/packageName和不同的谷歌云消息传递项目Id。谷歌服务。Json必须放在/app/google-services。Json,而不是flavor文件夹。

有没有办法让谷歌的服务。Json配置不同的许多口味?


当前回答

放置你的“谷歌服务”。Json”文件分别在app/src/flavors下 然后是构建。Gradle的应用程序,在android下添加以下代码

gradle.taskGraph.beforeTask { Task task ->
        if (task.name ==~ /process.*GoogleServices/) {
            android.applicationVariants.all { variant ->
                if (task.name ==~ /(?i)process${variant.name}GoogleServices/) {
                    copy {
                        from "/src/${variant.flavorName}"
                        into '.'
                        include 'google-services.json'
                    }
                }
            }
        }
    }

其他回答

1)。谷歌提供什么服务?Json真的有用吗?

关注这个:https://stackoverflow.com/a/31598587/2382964

2)。谷歌是如何服务的。Json文件影响你的android工作室项目?

关注这个:https://stackoverflow.com/a/33083898/2382964

简单来说就是第二个url,如果你添加google-services。Json在你的项目中必须有一个自动生成的google-services文件夹在这个路径下调试变量

app/build/generated/res/google-services/debug/values/values.xml

3)。该怎么做,才能把事情做好?

在project_level构建中添加google-services依赖项。Gradle,如果你使用app_compact库,你也可以使用3.0.0版本。

// Top-level build.gradle file
classpath 'com.google.gms:google-services:2.1.2'

现在在app_level build中。你必须在底部添加Gradle。

// app-level build.gradle file
apply plugin: 'com.google.gms.google-services'

注意:在gradle文件底部添加这一行非常重要。否则Gradle构建不会给你任何错误,但它不能正常工作。

4)。谷歌服务该放在哪里?Json文件在你的结构。

如果你没有build_flavor,就把它放在inside /app/google-service中。json文件夹。

情况2.)如果你有多个build_flavor并且你有不同的google_services。Json文件放在app/src/build_flavor/google-service.json中。

情况3.)如果你有多个build_flavor和单个google_services。Json文件放在app/google-service.json中。

我正在使用谷歌服务。json文件,从这里创建:https://developers.google.com/mobile/add?platform=android&cntapi=gcm&cnturl=https:%2F%2Fdevelopers.google.com%2Fcloud-messaging%2Fandroid%2Fclient&cntlbl=Continue%20Adding%20GCM%20Support&%3Fconfigured%3Dtrue

在json结构中,有一个称为客户机的json数组。如果你有多种口味,只需在这里添加不同的属性。

{
  "project_info": {
    "project_id": "PRODJECT-ID",
    "project_number": "PROJECT-NUMBER",
    "name": "APPLICATION-NAME"
  },
  "client": [
    {
      "client_info": {
        "mobilesdk_app_id": "1:PROJECT-NUMBER:android:HASH-FOR-FLAVOR1",
        "client_id": "android:PACKAGE-NAME-1",
        "client_type": 1,
        "android_client_info": {
          "package_name": "PACKAGE-NAME-1"
        }
      },
      "oauth_client": [],
      "api_key": [],
      "services": {
        "analytics_service": {
          "status": 1
        },
        "cloud_messaging_service": {
          "status": 2,
          "apns_config": []
        },
        "appinvite_service": {
          "status": 1,
          "other_platform_oauth_client": []
        },
        "google_signin_service": {
          "status": 1
        },
        "ads_service": {
          "status": 1
        }
      }
    },
    {
      "client_info": {
        "mobilesdk_app_id": "1:PROJECT-NUMBER:android:HASH-FOR-FLAVOR2",
        "client_id": "android:PACKAGE-NAME-2",
        "client_type": 1,
        "android_client_info": {
          "package_name": "PACKAGE-NAME-2"
        }
      },
      "oauth_client": [],
      "api_key": [],
      "services": {
        "analytics_service": {
          "status": 1
        },
        "cloud_messaging_service": {
          "status": 2,
          "apns_config": []
        },
        "appinvite_service": {
          "status": 1,
          "other_platform_oauth_client": []
        },
        "google_signin_service": {
          "status": 1
        },
        "ads_service": {
          "status": 1
        }
      }
    }
  ],
  "client_info": [],
  "ARTIFACT_VERSION": "1"
}

在我的项目中,我使用相同的项目id,当我在上面的url中添加第二个package-name时,谷歌为我提供了一个包含json-data中的多个客户端的文件。

你有很多口味,所以这意味着你会有很多不同的包装id,对吧?所以,只需转到您设置/生成json文件和配置每个包名的页面。所有这些都将添加到json文件中。

我现在很懒发布照片,但基本上:

访问https://developers.google.com/mobile/add 选择平台 选择你的应用 重要:输入你的口味包名称字段“android包名称” ... 继续获取配置文件。下载它!

当配置文件时,您可以看到谷歌向您显示服务器API密钥+发送方ID。所有包装(口味)都是一样的

最后,您只需要一个json文件来处理所有类型。

这里还有一个问题,当你注册获得注册令牌时,你必须测试,检查每个口味是否有差异。我没有触及它,但它认为应该是不同的。现在太晚了,我很困:)希望它能帮助!

根据ahmed_khan_89的回答,您可以将“复制代码”放在产品风味中。

productFlavors {
    staging {
        applicationId = "com.demo.staging"

        println "Using Staging google-service.json"
        copy {
            from 'src/staging/'
            include '*.json'
            into '.'
        }
    }
    production {
        applicationId = "com.demo.production"

        println "Using Production google-service.json"
        copy {
            from 'src/production/'
            include '*.json'
            into '.'
        }
    }
}

这样你就不必手动切换设置了。

简短的回答:

实现: 默认情况下,您应该复制google-services。Json到app dir。

如果想要获得其他效果,可以复制谷歌的服务。Json到app/src/{flavor-name}目录

测试: 尝试构建,打开构建选项卡,然后使用解析json文件检查输出消息:.....