Friday 25 April 2014

GCM push Notification good link

use this link : http://fundroiding.wordpress.com/2012/06/29/google-cloud-messaging-for-android-gcm-simple-tutorial/

Google Cloud Messaging For Android (GCM) Simple Tutorial

Important: C2DM has been officially deprecated as of June 26, 2012.
It has been replaced by Google Cloud Messaging For Android (GCM)
This Tutorial will guide you how to create a sample simple application using the GCM functionality,
This demo will help you registering and unRegistering android device from GCM server
Getting your Sender ID
  • STEP 1.  Register Here .
  • STEP 2.  Click Create project. Your browser URL will change to something like:
    " https://code.google.com/apis/console/#project:4815162342 "
    Take note of the value after#project: (4815162342 in this example). This is your project ID, and it will be used later on as the GCM sender ID. This Id will be used by the Android Device while Registering for Push Notification.
  • STEP 3. Choose Service tab from the left side menu on the web page. and turn on ” Google Cloud Messaging for Android “
  • STEP 4. Go to API Access tab from the left menu of web page.
press Create new Server key and note down the generated key

CREATING APP FOR GCM
  1. Update ADT plugin 20 .
  2. update SDK > install Extras > Google Cloud Messaging for Android Library.
  3. Add gcm.jar to libs folder.(will be in the  android_sdk/extras/google/gcm after updating ADT and SDK)
  • STEP 5. Create A new Project in Android with the following specifications
Android Version 2.2
Package   =  com.sagar.gcma
Main Activity  =  PushAndroidActivity
  • Source Code for PushAndroidActivity.java:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package com.sagar.gcma;
import static com.sagar.gcma.CommonUtilities.SENDER_ID;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import com.google.android.gcm.GCMRegistrar;
public class PushAndroidActivity extends Activity {
private String TAG = "** pushAndroidActivity **";
private TextView mDisplay;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
checkNotNull(SENDER_ID, "SENDER_ID");
GCMRegistrar.checkDevice(this);
GCMRegistrar.checkManifest(this);
setContentView(R.layout.main);
mDisplay = (TextView) findViewById(R.id.display);
final String regId = GCMRegistrar.getRegistrationId(this);
Log.i(TAG, "registration id =====  "+regId);
if (regId.equals("")) {
GCMRegistrar.register(this, SENDER_ID);
} else {
Log.v(TAG, "Already registered");
}
mDisplay.setText("ffffff        "+regId);
}
private void checkNotNull(Object reference, String name) {
if (reference == null) {
throw new NullPointerException(
getString(R.string.error_config, name));
}
}
@Override
protected void onPause() {
super.onPause();
GCMRegistrar.unregister(this);
}
}

  • Source code for GCMIntentService.java :
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package com.sagar.gcma;
import android.app.Notification; import android.app.Notification.Builder; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.os.Build; import android.os.Bundle; import android.util.Log; import com.google.android.gcm.GCMBaseIntentService; public class GCMIntentService extends GCMBaseIntentService { public static final String GCM_SENDER_ID = "378946919077"; public GCMIntentService() { super(GCM_SENDER_ID); } @SuppressWarnings("deprecation") @Override protected void onMessage(Context context, Intent intent) { try { final Bundle bundle = intent.getExtras(); String message = bundle.getString("message"); Log.i(TAG, "message: " + message); // MainActivity mainActivity = MainActivity.getMainActivity(); // if (mainActivity != null) { // mainActivity.onPushMessage(message); // clearPushMessage(); // } else // Intent notificationIntent = new Intent(this, MyClass.class); // PendingIntent contentIntent = PendingIntent.getActivity(this, 0, // notificationIntent, 0); Intent mIntent = new Intent(context, LoginActivity.class); mIntent.putExtra("pushmessage", message); mIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); final PendingIntent contentIntent = PendingIntent.getActivity( context, (int) System.currentTimeMillis(), mIntent, 0); if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) { setPushMessage(message); final Notification notification = new Notification(); final Object systemService = context .getSystemService(Context.NOTIFICATION_SERVICE); final NotificationManager notificationMgr = (NotificationManager) systemService; notification.flags |= Notification.FLAG_AUTO_CANCEL; // notification.defaults |= Notification.DEFAULT_SOUND; notification.defaults = Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE; notification.vibrate = new long[] { 0, 100, 200, 300 }; notification.contentIntent = contentIntent; notification.icon = R.drawable.ic_launcher; notification.tickerText = context.getString(R.string.app_name); String appName = context.getResources().getString( R.string.app_name); notification.setLatestEventInfo(this, appName, message, contentIntent); // notification.setLatestEventInfo(context, contentTitle, // contentText, contentIntent); int ss = (int) System.currentTimeMillis(); notificationMgr.notify(ss, notification); } else { NotificationManager m_notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); Builder m_builder = new Notification.Builder(this); m_builder .setContentTitle(context.getString(R.string.app_name)) .setContentText(message) .setSmallIcon(R.drawable.ic_launcher) .setContentIntent(contentIntent) .setAutoCancel(true) .setVibrate(new long[] { 0, 100, 200, 300 }) .setDefaults( Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE); String appName = context.getResources().getString( R.string.app_name); Notification m_notification = new Notification.BigTextStyle( m_builder).bigText(message).build(); m_notification.setLatestEventInfo(this, appName, message, contentIntent); int ss = (int) System.currentTimeMillis(); m_notificationManager.notify(ss, m_notification); } } catch (Exception e) { e.printStackTrace(); } } private void setPushMessage(String message) { final SharedPreferences mSharedPreferences = getSharedPreferences( "PushNotification", 0); final SharedPreferences.Editor mEditor = mSharedPreferences.edit(); mEditor.putString("message", message); mEditor.commit(); } public void clearPushMessage() { final SharedPreferences mSharedPreferences = getSharedPreferences( "PushNotification", 0); final SharedPreferences.Editor mEditor = mSharedPreferences.edit(); mEditor.putString("message", null); mEditor.commit(); } private static final String TAG = "GCMIntentService"; @Override protected void onRegistered(Context arg0, String registrationId) { Log.i(TAG, "Device registered: regId = " + registrationId); } @Override protected void onUnregistered(Context arg0, String arg1) { Log.i(TAG, "unregistered = " + arg1); } @Override protected void onError(Context arg0, String errorId) { Log.i(TAG, "Received error: " + errorId); } @Override protected boolean onRecoverableError(Context context, String errorId) { return super.onRecoverableError(context, errorId); } }

  • Source Code for CommonUtilities.java
?
1
2
3
4
5
6
7
package com.sagar.gcma;
public final class CommonUtilities {
static final String SENDER_ID = "515850168860";
}
  • Source Code for AndroidManifest.xml
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<?xml version="1.0" encoding="utf-8"?>
package="com.sagar.gcma"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="16" />
<permission
android:name="com.sagar.gcma.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.sagar.gcma.permission.C2D_MESSAGE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".PushAndroidActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name="com.google.android.gcm.GCMBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.sagar.gcma" />
</intent-filter>
</receiver>
<service android:name=".GCMIntentService" />
</application>
</manifest>

  • SourceCode of main.xml
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre>
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="@+id/display"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#ffffff" />
</ScrollView>
<pre>
Package explorer ScreenShot
Logcat ScreenShot
Important links: