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

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...

SearchView Widget in Android

Android SearchView widget, users enter search queries and can then submit a request for the search provider offers search user interface. This survey shows a list of suggestions or proposals, or to launch into the existing results and users will allow you to get the result. Configuring the search widget Basic XML < android.support.v7.widget.SearchView android:layout_width = "match_parent" android:layout_height = "wrap_content" android:text = "Hello World!" /> Search widget provides the same functionality as the search dialog. It has been operating in accordance with the user to perform a search, and it can search suggestions and voice search. Action Bar will search widget not an option for you, instead of you can put the layout of the search widget on the ground. Create new android project Configure dependencies. add follow code dependencies { // Other dependencies .... compile 'com.a...

Tutorial Learn to Make Push Notification on Android

Android provides alarm management class for this purpose. To use this class, you need to instantiate an object in this category from getting Android system through getSystemService () method. The next thing you need to do is to create a context and intention of waiting intention to pass as a parameter. Pending the intention of giving another application that empowers you to perform the action you set for yourself, as if another program. The last thing you need to do TE's to setLatestEventInfo Method of Notice Class calls and pass umrl pending configuration Together izpolnjeni notice subject and body details. His syntax Beseda given below. And then finally call umrl Knowledge Method of NotificationManager Class. MainActivity.java import android.app.Notification; import android.app.NotificationManager; import android.content.Context; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; impo...