Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do I need to add the configuration file to my project to set up GCM correctly?

https://developers.google.com/cloud-messaging/android/client I've read this article. And it says that I need to download and add the configuration file to my project. But they do not explain, why should I do this? Won't it work without adding the configuration file?

like image 387
Semyon Avatar asked Jul 14 '15 16:07

Semyon


People also ask

What is the use of app config in c#?

App. Config is an XML file that is used as a configuration file for your application. In other words, you store inside it any setting that you may want to change without having to change code (and recompiling). It is often used to store connection strings.


2 Answers

If you do not generate and add the proper configuration file to your project, then yes, you will not be able to get push notification working.

In previous version of the SDK, you had to configure the senderID manually in your code.

Now, with GCM 3, Google has unified the way to configure Google Service. Their portal now let you configure all the service you want to enable for your app. Once done, it generates a configuration file that contains the parameter you need from the mobile to use the service.

Without that info, and most notably the project number, Google will be unable to generate InstanceID that allows receiving push with your senderID and Server API key. This is a way to match the client credential on Android, with the server credentials on the backend that allows you to send push notifications to your Android app and your app only.

How does it works ?

The configuration file is used by the google-services Gradle Android plugin to generate the needed configuration resources. It means you are using it if you have this in your build.gradle file:

apply plugin: 'com.google.gms.google-services'

Then, it will generate the resource R.string.gcm_defaultSenderId that you see in Google GCM example code:

InstanceID instanceID = InstanceID.getInstance(this);
String token = instanceID.getToken(getString(R.string.gcm_defaultSenderId),
        GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);

However, if you decide to hardcode the SenderID in your code and pass it to the instanceID.getToken method, then indeed, it will work. Having the JSON configuration file is a convenience provided by Google. It is cleaner as it centralize all your Google Service parameter.

The link of your app to project / senderID is mandatory, but if you really do not like the idea of having this JSON Google server configuration file you can work around it by putting the needed values elsewhere manually.

like image 195
Mickaël Rémond Avatar answered Oct 17 '22 01:10

Mickaël Rémond


I am also working on multiple GCM-projects but still using Eclipse, Eclipse-ADT-plugin and the old project structure and I did not include this config-file into my projects. I also was confused at the beginning and hence tried it in a Android-Studio project using Gradle but the config file was not necessary either.

So Rémond´s answer might make sense but for me everything is working fine (including generating InstanceID-Token and also Topic-Messaging is working).

So in fact, it should work without adding the file and you do not need this file anyway.

like image 29
sschmid Avatar answered Oct 17 '22 03:10

sschmid