Skip to main content

Push Notification Using Google Cloud Messaging on Android

This is supposed to pay notifications tutorial which uses Android Studio as IDE, with target device running Android 4.0.4 or higher.

Google Cloud Messaging (GCM) The services offered by Google, which allows developers to implement their program Push messages. Using GCM, developers do not need to apply their method of data sent from the server to the client program.

Configuring the Project in Google Developers Console

Configure your project

Add the dependency to your project-level build.gradle:
classpath 'com.google.gms:google-services:3.0.0'
apply plugin: 'com.google.gms.google-services'
dependencies {
  compile "com.google.android.gms:play-services-gcm:10.2.0"
}

Add the following to your application's manifest:
AndroidManifest.xml
<manifest package="com.example.gcm" ...>

    <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17"/>
    <uses-permission android:name="android.permission.WAKE_LOCK" />

    <application ...>
        <receiver
            android:name="com.google.android.gms.gcm.GcmReceiver"
            android:exported="true"
            android:permission="com.google.android.c2dm.permission.SEND" >
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <category android:name="com.example.gcm" />
            </intent-filter>
        </receiver>
        <service
            android:name="com.example.MyGcmListenerService"
            android:exported="false" >
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            </intent-filter>
        </service>
        <service
            android:name="com.example.MyInstanceIDListenerService"
            android:exported="false">
            <intent-filter>
                <action android:name="com.google.android.gms.iid.InstanceID" />
            </intent-filter>
        </service>
        <service
            android:name="gcm.play.android.samples.com.gcmquickstart.RegistrationIntentService"
            android:exported="false">
        </service>
    </application>

</manifest>

Create a Registration Service
Registration must be done in the background thread as the process can take some time depending on the network connection. Because the record does not need input from the user, with IntentService experience ideal for this task.
RegistrationService.java
public class RegistrationIntentService extends IntentService {
    @Override
    public void onHandleIntent(Intent intent) {

        InstanceID instanceID = InstanceID.getInstance(this);
        String token = instanceID.getToken(getString(R.string.gcm_defaultSenderId),
                GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
    }
}
Registration tokens are refreshed periodically. As a result, each Android app that uses the GCM can handle a InstanceIDListenerService refresh it should be. Therefore, TokenRefreshListenerService.java create a new Java file and make it a subclass of InstanceIDListenerService. Inside the classroom onTokenRefresh method, we all need to do is simply intent and begin the process of re-registration from the registration service startService method.
MyInstanceIDListenerService.java
@Override
public void onTokenRefresh() {
    // Fetch updated Instance ID token and notify our app's server of any changes (if applicable).
    Intent intent = new Intent(this, RegistrationIntentService.class);
    startService(intent);
}

Add TokenRefrshListenerService.java to AndroidManifest.xml
<service
    android:name=".MyInstanceIDListenerService"    android:exported="false">
    <intent-filter>
        <action android:name="com.google.android.gms.iid.InstanceID" />
    </intent-filter>
</service>

Popular posts from this blog

Tutorial Firebase Push Notification With Image on Android

Firebase cloud messaging (FCM) is a new version of Google Cloud Messaging (GCM). Using FCM you can send notification messages to your client application in order to drive user engagement. Notification messages can contain an optional data payload, which is delivered when users tap on the notification. Use notification messages when you want Firebase cloud messaging (FCM) to handle displaying a notification on your client app’s behalf. Use data messages when you want to process the messages on your client app. Create a new Android 1. Create a new Android Project in Android Studio. Give it a name and select the Minimum SDK on which your app will run on. I chose API 16 : Android 4.1 (JELLY_BEAN). 2. When you are prompted to add an activity to your application choose Blank Activity and click on next button. 3. In the next step click on Finish and in few seconds your application should be loaded in Android Studio. 4. Open build.gradle(Module:App) file of your application and add the ...

Learn to Make Ground Overlay Google Maps on Android

A land overlay is an image that is a permanent map. Unlike markers , ground cover on the ground to accommodate the screen oriented, turn, tilt, or zoom the map to change the orientation of the image. Terrestrial overlays are useful when you want to define an image in a region on the map. If you want to add rich images that cover a large part of the map, you should consider covering tiles. Create a new android maps project 1. Start Android Studio. 2. Create a new project as follows:  If you see the Welcome to Android Studio dialog, choose Start a new Android Studio project, available under 'Quick Start' on the right of the dialog.  If you see the Welcome to Android Studio dialog, choose Start a new Android Studio project, available under 'Quick Start' on the right of the dialog. Otherwise, click File in the Android Studio menu bar, then New, New Project. 3. Enter your app name, company domain, and project location, as prompted. Then click Next. ...

Apps created on Android

For applications on Android (for beginners) Start - for those of you who love DIY Android and the desire to create their own programs. The study of the application of decisions is not easy, but for a beginner it can be found. Some developers of applications for Android, to provide the tools to create applications for beginners, of course, these tools can be downloaded directly from Google Play on your Android gadget. Here are 5 tools manufacturers Android apps for beginners. 1. Appiventor Basically this program is used by inexperienced developers, and has now received belongs to the Massachusetts Institute of Technology. Tools such as this requires its own understands the language of a program or "coding", but do not worry, because this app also has video lessons. 2. PhoneGap For those of you who are effective using HTML, CSS or JavaScript simply using Fungab. Fungab not only used to create mobile applications for Android, but also for mobile phone application...