Google Cloud Messaging (GCM) is a service that allows you to send push notifications from your server to your users, and also to receive messages from devices on the same connection. The GCM service handles all aspects of queueing of messages and delivery to the target Android application running on the target device. GCM is the same great product from Google but now under a new name: Firebase Cloud Messaging or FCM for short.
This tutorial guides you through how to implement an App engine with FCM.
1. Add Google Cloud Module:
Make a new project to generate a token which will be used for sending and receiving notifications from firebase. You can create a new project from my previous post to get your token or if you are having your own project then just implement the code to your project mentioned in this post to generate the token which will be used further.
To add the backend to your existing or new Android app, open Android Studio and navigate to "File => New Module" or right-click on your project and choose "New => Module".
In the "New Module" dialog that appears, choose "Google Cloud Module":
Then choose "App Engine Backend with Google Cloud Messaging" from first drop-down list.
Enter the module/package names for your new backend, and choose the "client" module in the project which contains your Android app. You can enter the name of your choice for your new backend. The client module will be set up to call your newly generated backend. The module name which you've entered above (marked with red 1) will be used in your Android Studio project. The package name (marked with red 2) will be used for all classes imported from this template and (reversed) for Endpoints API namespaces. In turn, Endpoints API namespaces will be used to derive the name of the autogenerated Android client libraries. This ensures that the names of generated client libraries will match your package name.
2. Debug the backend locally:
As soon as the backend module is added to your project and Gradle sync finishes, a new run configuration with your backend's module name should be created. Rebuild your project (via "Build => Rebuild Project") and launch this run configuration. It will invoke appengineRun task in Gradle plug-in for App Engine, which in turn will start the local App Engine Java development server. Navigate to http://localhost:8080. You should see the following page:
3. Connect your app with your backend:
To test the application, you need a Google Cloud Messaging API key. Follow the instructions to create a google-services.json configuration file and API key. Open the <backend>/src/main/webapp/WEB-INF/appengine-web.xml file (it should have been opened by default when you generated the backend), and copy the generated API key into appengine-web.xml file, replacing
<property name="gcm.api.key" value="YOUR_KEY_HERE"/>
with
<property name="gcm.api.key" value="AIza..."/>
4. Register your device:
Before any messages can be sent from your backend to the devices, these devices need to be registered with your backend. When you added this backend module to your project, the required permissions, needed by Firebase Cloud Messaging have been added into the Android manifest of your app, and the required build dependencies have been added to your app's build.gradle file.
Furthermore, a RegistrationEndpoint Cloud Endpoints API has been automatically generated for you, so that you could start calling this endpoint from your Android app to register devices with your new backend. Set up your Google Cloud Messaging Client to obtain a registration token, register your android device with your new backend, and set up the listener services to show push notifications from the GCM backend.
To send your newly obtained GCM registration token to the server, use the following code snippet which you should invoke from the RegistrationIntentService you created while setting up the client:
private void sendRegistrationToServer(String token) throws IOException {
Registration.Builder builder = new Registration.Builder(AndroidHttp.newCompatibleTransport(),
new AndroidJsonFactory(), null)
// Need setRootUrl and setGoogleClientRequestInitializer only for local testing,
// otherwise they can be skipped
.setRootUrl("http://<YOUR_LOCAL_IP>:8080/_ah/api/")
.setGoogleClientRequestInitializer(new GoogleClientRequestInitializer() {
@Override
public void initialize(AbstractGoogleClientRequest<?> abstractGoogleClientRequest)
throws IOException {
abstractGoogleClientRequest.setDisableGZipContent(true);
}
});
Registration regService = builder.build();
regService.register(token).execute();
}
5. Test it:
If you have added a RegistrationIntentService invocation to one of your Android app activities and set up the google-services.json configuration file containing your Google Developers Console project number, you are ready to test the device registration with your backend locally. Now launch your backend locally as described in point-2. and ensure that you can access it via http://localhost:8080. If you can access the backend locally, change the run configuration back to your Android app and run the Android emulator.
Testing on a physical device with a local development server requires minor changes to your configuration. You must make your development server accessible to the network by setting it to listen to external connections. You can do this by editing the build.gradle for the backend project and setting the httpAddress.
appengine {
....
httpAddress = "0.0.0.0"
....
}
You must also change the endpoint root url to point to your computer's ip address when creating the endpoint(In your RegistrationIntentService).
Registration.Builder builder = new Registration.Builder(AndroidHttp.newCompatibleTransport(),
new AndroidJsonFactory(), null)
.setRootUrl("http://<YOUR_COMPUTER_ADDRESS>:8080/_ah/api/")
....
Now to see notification in your physical device make sure your backend is running locally as shown in point-2. and your app running in your physical device. Then from your backend enter a message and click Send Message>>, then you will be able to see a notification in your device as follows:
6. Deploy your backend:
If your backend is working fine locally, you can now deploy it to Google App Engine.
- Stop the backend, if it is running locally, by selecting Run => Stop.
- Run Build => Deploy Module to App Engine.
- In the Deploy to App Engine dialog, select your module. From the Deploy To: dropdown list, choose your Google Console Project which you have create to get your api_key or if you have not created your one then choose "Click here to create a new Google Developers Console project." This will open Google Developers Console. If you are running this task for the first time, you will be prompted to sign-in with your Google Account. Choose an account and sign in.
- Create a new project and switch back to the Deploy to App Engine dialog in Android Studio.
Now update your src/main/webapp/WEB-INF/appengine-web.xml file's <application> property and replace myApplicationId with the ID of the project that you just created. This will be important if you try to deploy from the command line.
- Click the Refresh button in the bottom right corner of the Deploy To: dropdown list and then select the project you just created.
- Click Deploy. You can monitor the status of your deployment in the Android Studio console.
7. Testing against your deployed backend:
Once you have deployed your backend to App Engine, you can connect your Android app to it by modifying the RegistrationIntentService class explained in point-4. above. and replace particular lines:
Registration.Builder builder = new Registration.Builder(AndroidHttp.newCompatibleTransport(),
new AndroidJsonFactory(), null)
// Need setRootUrl and setGoogleClientRequestInitializer only for local testing,
// otherwise they can be skipped
.setRootUrl("http://10.0.2.2:8080/_ah/api/")
.setGoogleClientRequestInitializer(new GoogleClientRequestInitializer() {
@Override
public void initialize(AbstractGoogleClientRequest<?> abstractGoogleClientRequest)
throws IOException {
abstractGoogleClientRequest.setDisableGZipContent(true);
}
});
with these lines
Registration.Builder builder = new Registration.Builder(AndroidHttp.newCompatibleTransport(),
new AndroidJsonFactory(), null)
.setRootUrl("https://android-app-backend.appspot.com/_ah/api/");
where android-app-backend corresponds to your own Project ID created above in 6.
8. At this point you should be all set to run your Android app in an emulator or on the physical device and send messages from your backend which you have created and deployed just now.
You can access your backend at https://android-app-backend.appspot.com , where android-app-backend corresponds to your own Project ID created above in 6 and successfully communicate with your new App Engine backend!!
That's all. Comment if any queries.
This tutorial guides you through how to implement an App engine with FCM.
1. Add Google Cloud Module:
Make a new project to generate a token which will be used for sending and receiving notifications from firebase. You can create a new project from my previous post to get your token or if you are having your own project then just implement the code to your project mentioned in this post to generate the token which will be used further.
To add the backend to your existing or new Android app, open Android Studio and navigate to "File => New Module" or right-click on your project and choose "New => Module".
In the "New Module" dialog that appears, choose "Google Cloud Module":
Then choose "App Engine Backend with Google Cloud Messaging" from first drop-down list.
Enter the module/package names for your new backend, and choose the "client" module in the project which contains your Android app. You can enter the name of your choice for your new backend. The client module will be set up to call your newly generated backend. The module name which you've entered above (marked with red 1) will be used in your Android Studio project. The package name (marked with red 2) will be used for all classes imported from this template and (reversed) for Endpoints API namespaces. In turn, Endpoints API namespaces will be used to derive the name of the autogenerated Android client libraries. This ensures that the names of generated client libraries will match your package name.
2. Debug the backend locally:
As soon as the backend module is added to your project and Gradle sync finishes, a new run configuration with your backend's module name should be created. Rebuild your project (via "Build => Rebuild Project") and launch this run configuration. It will invoke appengineRun task in Gradle plug-in for App Engine, which in turn will start the local App Engine Java development server. Navigate to http://localhost:8080. You should see the following page:
3. Connect your app with your backend:
To test the application, you need a Google Cloud Messaging API key. Follow the instructions to create a google-services.json configuration file and API key. Open the <backend>/src/main/webapp/WEB-INF/appengine-web.xml file (it should have been opened by default when you generated the backend), and copy the generated API key into appengine-web.xml file, replacing
<property name="gcm.api.key" value="YOUR_KEY_HERE"/>
with
<property name="gcm.api.key" value="AIza..."/>
4. Register your device:
Before any messages can be sent from your backend to the devices, these devices need to be registered with your backend. When you added this backend module to your project, the required permissions, needed by Firebase Cloud Messaging have been added into the Android manifest of your app, and the required build dependencies have been added to your app's build.gradle file.
Furthermore, a RegistrationEndpoint Cloud Endpoints API has been automatically generated for you, so that you could start calling this endpoint from your Android app to register devices with your new backend. Set up your Google Cloud Messaging Client to obtain a registration token, register your android device with your new backend, and set up the listener services to show push notifications from the GCM backend.
To send your newly obtained GCM registration token to the server, use the following code snippet which you should invoke from the RegistrationIntentService you created while setting up the client:
private void sendRegistrationToServer(String token) throws IOException {
Registration.Builder builder = new Registration.Builder(AndroidHttp.newCompatibleTransport(),
new AndroidJsonFactory(), null)
// Need setRootUrl and setGoogleClientRequestInitializer only for local testing,
// otherwise they can be skipped
.setRootUrl("http://<YOUR_LOCAL_IP>:8080/_ah/api/")
.setGoogleClientRequestInitializer(new GoogleClientRequestInitializer() {
@Override
public void initialize(AbstractGoogleClientRequest<?> abstractGoogleClientRequest)
throws IOException {
abstractGoogleClientRequest.setDisableGZipContent(true);
}
});
Registration regService = builder.build();
regService.register(token).execute();
}
5. Test it:
If you have added a RegistrationIntentService invocation to one of your Android app activities and set up the google-services.json configuration file containing your Google Developers Console project number, you are ready to test the device registration with your backend locally. Now launch your backend locally as described in point-2. and ensure that you can access it via http://localhost:8080. If you can access the backend locally, change the run configuration back to your Android app and run the Android emulator.
Testing on a physical device with a local development server requires minor changes to your configuration. You must make your development server accessible to the network by setting it to listen to external connections. You can do this by editing the build.gradle for the backend project and setting the httpAddress.
appengine {
....
httpAddress = "0.0.0.0"
....
}
You must also change the endpoint root url to point to your computer's ip address when creating the endpoint(In your RegistrationIntentService).
Registration.Builder builder = new Registration.Builder(AndroidHttp.newCompatibleTransport(),
new AndroidJsonFactory(), null)
.setRootUrl("http://<YOUR_COMPUTER_ADDRESS>:8080/_ah/api/")
....
Now to see notification in your physical device make sure your backend is running locally as shown in point-2. and your app running in your physical device. Then from your backend enter a message and click Send Message>>, then you will be able to see a notification in your device as follows:
6. Deploy your backend:
If your backend is working fine locally, you can now deploy it to Google App Engine.
- Stop the backend, if it is running locally, by selecting Run => Stop.
- Run Build => Deploy Module to App Engine.
- In the Deploy to App Engine dialog, select your module. From the Deploy To: dropdown list, choose your Google Console Project which you have create to get your api_key or if you have not created your one then choose "Click here to create a new Google Developers Console project." This will open Google Developers Console. If you are running this task for the first time, you will be prompted to sign-in with your Google Account. Choose an account and sign in.
- Create a new project and switch back to the Deploy to App Engine dialog in Android Studio.
Now update your src/main/webapp/WEB-INF/appengine-web.xml file's <application> property and replace myApplicationId with the ID of the project that you just created. This will be important if you try to deploy from the command line.
- Click the Refresh button in the bottom right corner of the Deploy To: dropdown list and then select the project you just created.
- Click Deploy. You can monitor the status of your deployment in the Android Studio console.
7. Testing against your deployed backend:
Once you have deployed your backend to App Engine, you can connect your Android app to it by modifying the RegistrationIntentService class explained in point-4. above. and replace particular lines:
Registration.Builder builder = new Registration.Builder(AndroidHttp.newCompatibleTransport(),
new AndroidJsonFactory(), null)
// Need setRootUrl and setGoogleClientRequestInitializer only for local testing,
// otherwise they can be skipped
.setRootUrl("http://10.0.2.2:8080/_ah/api/")
.setGoogleClientRequestInitializer(new GoogleClientRequestInitializer() {
@Override
public void initialize(AbstractGoogleClientRequest<?> abstractGoogleClientRequest)
throws IOException {
abstractGoogleClientRequest.setDisableGZipContent(true);
}
});
with these lines
Registration.Builder builder = new Registration.Builder(AndroidHttp.newCompatibleTransport(),
new AndroidJsonFactory(), null)
.setRootUrl("https://android-app-backend.appspot.com/_ah/api/");
where android-app-backend corresponds to your own Project ID created above in 6.
8. At this point you should be all set to run your Android app in an emulator or on the physical device and send messages from your backend which you have created and deployed just now.
You can access your backend at https://android-app-backend.appspot.com , where android-app-backend corresponds to your own Project ID created above in 6 and successfully communicate with your new App Engine backend!!
That's all. Comment if any queries.
No comments:
Post a Comment