Include the library of GCM.jar file in your project
public class GCMIntentService extends GCMBaseIntentService {
@SuppressWarnings("hiding")
private static final String TAG = "GCMIntentService";
public GCMIntentService() {
super("123456");
}
/**
* Issues a notification to inform the user that server has sent a message.
*/
private static void generateNotification(Context context, String message)
{
int icon = R.drawable.icon;
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);
String title = context.getString(R.string.app_name);
Intent notificationIntent = new Intent(context, ABCCCC.class);
// set intent so it does not start a new activity
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent =
PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, notification);
}
@Override
protected void onError(Context arg0, String arg1) {
// TODO Auto-generated method stub
}
@Override
protected void onMessage(Context arg0, Intent arg1) {
Log.e("GCM", "RECIEVED A MESSAGE");
Log.e("GCM", "RECIEVED A MESSAGE");
Log.e("GCM", "RECIEVED A MESSAGE");
Log.e("GCM", "RECIEVED A MESSAGE");
Log.e("GCM", "RECIEVED A MESSAGE");
Log.e("GCM", "RECIEVED A MESSAGE");
Log.e("GCM", "RECIEVED A MESSAGE");
generateNotification(getApplicationContext(),arg1.getStringExtra("message"));
}
@Override
protected void onRegistered(Context arg0, String regId) {
// TODO Auto-generated method stub
ConstantData.DEVICETOKEN=regId;
Log.e("MY_APP_TAG", "Registered: " + regId);
}
@Override
protected void onUnregistered(Context arg0, String arg1) {
// TODO Auto-generated method stub
}
}
SamplePushActivity extends Application {
/** Called when the activity is first created. */
public void onCreate() {
super.onCreate();
//setContentView(R.layout.main);
try{
//User device id...
TelephonyManager tManager = (TelephonyManager)getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE);
ConstantData.DEVICEID = tManager.getDeviceId();
// Log.e("Device id is:-", uid);
}catch (Exception e) {
// TODO: handle exception
}
try {
Log.e("IN GCM_SAMPLE_PUSH","aa gaya");
GCMRegistrar.checkDevice(this);
GCMRegistrar.unregister(this);
Log.e("info","unregistereddd....." + GCMRegistrar.getRegistrationId(this));
GCMRegistrar.checkManifest(this);
if (GCMRegistrar.isRegistered(this)) {
Log.e("info", GCMRegistrar.getRegistrationId(this));
}
final String regId = GCMRegistrar.getRegistrationId(this);
Log.e("REG_ID", regId);
if (regId.equals("")) {
GCMRegistrar.register(this, "CLENT SIDE ID");
Log.e("info", GCMRegistrar.getRegistrationId(this));
} else {
Log.e("info", "already registered as" + regId);
}
} catch (Exception e) {
// TODO: handle exception
}
}
}
make reciver in your manifest file
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="your project name" />
</intent-filter>
</receiver>
permission in manifest file
internet permission
make service in manifest file
<service android:name=".GCMIntentService" />
public class GCMIntentService extends GCMBaseIntentService {
@SuppressWarnings("hiding")
private static final String TAG = "GCMIntentService";
public GCMIntentService() {
super("123456");
}
/**
* Issues a notification to inform the user that server has sent a message.
*/
private static void generateNotification(Context context, String message)
{
int icon = R.drawable.icon;
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);
String title = context.getString(R.string.app_name);
Intent notificationIntent = new Intent(context, ABCCCC.class);
// set intent so it does not start a new activity
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent =
PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, notification);
}
@Override
protected void onError(Context arg0, String arg1) {
// TODO Auto-generated method stub
}
@Override
protected void onMessage(Context arg0, Intent arg1) {
Log.e("GCM", "RECIEVED A MESSAGE");
Log.e("GCM", "RECIEVED A MESSAGE");
Log.e("GCM", "RECIEVED A MESSAGE");
Log.e("GCM", "RECIEVED A MESSAGE");
Log.e("GCM", "RECIEVED A MESSAGE");
Log.e("GCM", "RECIEVED A MESSAGE");
Log.e("GCM", "RECIEVED A MESSAGE");
generateNotification(getApplicationContext(),arg1.getStringExtra("message"));
}
@Override
protected void onRegistered(Context arg0, String regId) {
// TODO Auto-generated method stub
ConstantData.DEVICETOKEN=regId;
Log.e("MY_APP_TAG", "Registered: " + regId);
}
@Override
protected void onUnregistered(Context arg0, String arg1) {
// TODO Auto-generated method stub
}
}
SamplePushActivity extends Application {
/** Called when the activity is first created. */
public void onCreate() {
super.onCreate();
//setContentView(R.layout.main);
try{
//User device id...
TelephonyManager tManager = (TelephonyManager)getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE);
ConstantData.DEVICEID = tManager.getDeviceId();
// Log.e("Device id is:-", uid);
}catch (Exception e) {
// TODO: handle exception
}
try {
Log.e("IN GCM_SAMPLE_PUSH","aa gaya");
GCMRegistrar.checkDevice(this);
GCMRegistrar.unregister(this);
Log.e("info","unregistereddd....." + GCMRegistrar.getRegistrationId(this));
GCMRegistrar.checkManifest(this);
if (GCMRegistrar.isRegistered(this)) {
Log.e("info", GCMRegistrar.getRegistrationId(this));
}
final String regId = GCMRegistrar.getRegistrationId(this);
Log.e("REG_ID", regId);
if (regId.equals("")) {
GCMRegistrar.register(this, "CLENT SIDE ID");
Log.e("info", GCMRegistrar.getRegistrationId(this));
} else {
Log.e("info", "already registered as" + regId);
}
} catch (Exception e) {
// TODO: handle exception
}
}
}
make reciver in your manifest file
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="your project name" />
</intent-filter>
</receiver>
permission in manifest file
internet permission
make service in manifest file
<service android:name=".GCMIntentService" />
No comments:
Post a Comment