Wednesday, August 19, 2015

Google Cloud Messaging

In the project that I am working on I had to figure out if I changed the applicationId, would an existing Google Cloud Messaging configuration continue to work.

So, I followed the instructions from Google: https://developers.google.com/cloud-messaging/android/client and set up GCM using the com.larkintuckerllc.helloandroid package name.

It is during the step get configuration file, we provide the package name.  At this point, is not clear if it is the applicationId or the actual package name that is relevant; I used the package name (same as applicationId in one of the applications).  The mysterious file created is:

google-services.json
{
  "project_info": {
    "project_id": "fromandbackagain",
    "project_number": "1009647335975",
    "name":"FromAndBackAgain"},
    "client":[
      {
        "client_info": {
          "client_id": "android:com.larkintuckerllc.helloandroid",
          "client_type":1,
          "android_client_info": {
            "package_name":"com.larkintuckerllc.helloandroid"
          }
        },
        "oauth_client":[],
        "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
          }
      }
    }
  ],
  "ARTIFACT_VERSION":"1"
}

Apparently it is the gradle plugin that parses this file and pushes these configuration values into your application.

Going through the documentation, I realized that it got fairly sparse on what appears to be a seemingly overly complicated endeavor.  So, I ended having to download their sample application and copy large chunks of code over to get it working: https://developers.google.com/cloud-messaging/android/start

Purely by accident (left off the plugin step), I figured out the connection between the google-services.json file and my application came in through some generated resources, e.g., R.string.gcm_defaultSenderId. Turns out that the value of this string is the project_number in the json file.

So, my interpretation of what is going on is that all this gobbly-gook with json files and gradle is to have the project_number (identifies clients that are allowed to listen) be automatically be injected as a string resource in the project (thus needing the package name - not applicationId).

The one relatively simple thing was to figure out how to send messages to the application: use a tool, e.g., Chrome application Postman to send HTTP post: https://developers.google.com/cloud-messaging/topic-messaging

Was able to reverse engineer the example code to determine the appropriate to/topics/global

Not sure that I ended up liking the code that I ended up pasting in, e.g,. seemed unnecessarily complicated, e.g., used anonymous classes at one point and there seem to be some extraneous classes.  At some point, I would likely choose to refactor it to my liking.  But are the changes anyway: https://github.com/larkintuckerllc/HelloAndroid/commit/3c023cca2c460a81c8f580174c13276b58e10067

No comments:

Post a Comment