Wednesday, 25 May 2016

GCM PushNotification in android

It's Simple
Let's go for GCM In Android


just follow following step and you get GCM notification easily


Step 1:-  Add Following Dependency


             compile 'com.google.android.gms:play-services:8.1.0'




Step 2:-  Android Manifiest.xml

Permission:-
<permission    android:name="${applicationId}.permission.C2D_MESSAGE"    android:protectionLevel="signature" />

<uses-permission android:name="${applicationId}.permission.C2D_MESSAGE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

<uses-permission android:name="com.example.googlemaps.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.GET_TASKS" />


Receiver
<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="${applicationId}" />
    </intent-filter>
</receiver>









Service



<service    android:name="${applicationId}.PushNotification"
    android:exported="false">
    <intent-filter>
        <action android:name="com.google.android.c2dm.intent.RECEIVE" />
    </intent-filter>
</service>






Step 3:- download 2 files and put in your code
GCMClientManager.java
 https://drive.google.com/open?id=0B1dF4YSBqUoobVlGWlZ5dXFmbVE
PushNotification.java
https://drive.google.com/open?id=0B1dF4YSBqUooTmZSRjNDcE1rQkE


Step 4:- register Notification


call it in your first page or login page and save notification token

private void registerNotification()
{
    GCMClientManager pushClientManager = new GCMClientManager(this, your project no);
    pushClientManager.registerIfNeeded(new GCMClientManager.RegistrationCompletedHandler() {
        @Override        public void onSuccess(String registrationId, boolean isNewRegistration) {
            Log.d("Registration id",registrationId);//eTtH4OOYfm8:APA91bG3FEjoR0gGfs78dvyA-1DFxlDSlf7F8DrN1vJjnvyGBi6ZbdwGcqAeu2piQ15YQWDNwdXILK-lQHMBKrkwYg2KOSEkW6QSDKTlQbZyOLst7vDXYCXsJL4unN4h2WYGJJeOjbtz        }
        @Override        public void onFailure(String ex) {
            Log.i("error-->>",""+ex);
            super.onFailure(ex);
        }
    });
}







No comments:

Post a Comment