The sender ID is used in the registration process for c2dm.but there has no receive messages or any register id.
//Akashc2dmActivity.java file
public class Akashc2dmActivity extends Activity implements OnClickListener {
Button Register,id;
private static PowerManager.WakeLock mWakeLock;
private static final String WAKELOCK_KEY = "C2DM_LIB";
@Override
protected void onCreate(Bundle savedInstanceState) {
if (mWakeLock == null) {
PowerManager pm = (PowerManager) Akashc2dmActivity.this
.getSystemService(Context.POWER_SERVICE);
mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
WAKELOCK_KEY);
}
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Register = (Button) findViewById(R.id.button);
Register.setOnClickListener(this);
}
public void sendRequest(View ignored) {
Log.d("Register", "hello");
Intent registrationIntent = new Intent(Constants.SEND_REGISTRATION_TO_GOOGLE);
iHaveNoClueWhatThisSettingDoesButItIsRequiredForTheIntentToWorkSoIBetterSetIt(registrationIntent);
registrationIntent.putExtra("sender", Constants.C2DM_APPLICATION_SERVER_ID);
startService(registrationIntent);
}
private void iHaveNoClueWhatThisSettingDoesButItIsRequiredForTheIntentToWorkSoIBetterSetIt(Intent registrationIntent) {
registrationIntent.putExtra("app", PendingIntent.getBroadcast(Akashc2dmActivity.this, 0, new Intent(), 0));
Log.d("app", ""+PendingIntent.getBroadcast(Akashc2dmActivity.this, 0, new Intent(), 0));
}
@Override
public void onClick(View v) {
if(v == Register){
sendRequest(Register);
}
}
}
// Constants.java files
public class Constants {
public static final String TAG = "c2dm";
public static final String REGISTRATION_INTENT = "com.google.android.c2dm.intent.REGISTER";
public static final String SEND_REGISTRATION_TO_GOOGLE = "com.google.android.c2dm.intent.REGISTER";
public static final String RECEIVED_REGISTRATION_ID_FROM_GOOGLE = "com.google.android.c2dm.intent.REGISTRATION";
public static final String RECEIVED_C2DM_MESSAGE_FROM_GOOGLE = "com.google.android.c2dm.intent.RECEIVE";
public static final String START_C2DM_SERVICE = "com.google.android.c2dm.intent.START_SERVICE";
public static final String C2DM_APPLICATION_SERVER_ID = "akash.singh55@gmail.com";
private Constants() {
}
}
//C2DMBroadcastReceiver.java file
public class C2DMBroadcastReceiver extends BroadcastReceiver {
@Override
public final void onReceive(Context context, Intent intent) {
if (Constants.RECEIVED_REGISTRATION_ID_FROM_GOOGLE.equals(intent.getAction())) {
Log.d(Constants.TAG, "Received a registration ID from Google.");
intent.setAction(Constants.REGISTRATION_INTENT);
intent.setClassName(context, RegistrationIDReceiver.class.getName());
} else if (Constants.RECEIVED_C2DM_MESSAGE_FROM_GOOGLE.equals(intent.getAction())) {
Log.d(Constants.TAG, "Received a C2DM message from Google.");
intent.setAction(Constants.START_C2DM_SERVICE);
intent.setClass(context, C2DMService.class);
}
context.startService(intent);
}
}
//C2DMService.java file
public class C2DMService extends Service {
@Override
public IBinder onBind(Intent intent) {
Log.e(com.technosoft.Akashc2dm.Constants.TAG, "I was awakend by C2DM!");
return new Binder();
}
}
//RegistrationException.java files
public class RegistrationException extends Exception {
private final String usedUrl;
private final int responseCode;
public RegistrationException(String message, String usedUrl, int responseCode) {
super(message);
this.usedUrl = usedUrl;
this.responseCode = responseCode;
}
public RegistrationException(String message, Throwable cause) {
super(message, cause);
usedUrl = "";
responseCode = 0;
}
public RegistrationException(String message, String usedUrl, IOException e) {
super(message, e);
this.usedUrl = usedUrl;
responseCode = 0;
}
@Override
public String getMessage() {
return String.format("%s; URL: %s; Response code: %d",
super.getMessage(), usedUrl, responseCode);
}
}
//RegistrationIDReceiver.java files
public class RegistrationIDReceiver extends IntentService {
NotificationManager nm;
public static final String EXTRA_UNREGISTERED = "unregistered";
private final RegistrationIDRegistrar registrar =
RegistrationIDRegistrar.getInstance();
private static final String EXTRA_ERROR = "error";
private static final String EXTRA_REGISTRATION_ID = "registration_id";
public RegistrationIDReceiver() {
super(Constants.C2DM_APPLICATION_SERVER_ID);
}
@Override
public final void onHandleIntent(Intent intent) {
Bundle extras = intent.getExtras();
String message = (String)extras.get("message");
Log.d("Tag", "msg:" + message);
Log.d(Constants.TAG, "Received intent to register");
final String registrationId = intent.getStringExtra(
EXTRA_REGISTRATION_ID);
notifcation(registrationId);
String error = intent.getStringExtra(EXTRA_ERROR);
if (error == null) {
registerDevice(registrationId);
} else {
handleRegistrationError(error);
}
}
private void notifcation(String id) {
nm = (NotificationManager) RegistrationIDReceiver.this.getSystemService(Context.NOTIFICATION_SERVICE);
CharSequence from = "Please Testing for Recive id";
CharSequence message = "received ID"+id;
Intent notifyintent = new Intent(getApplicationContext(),Akashc2dmActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0,notifyintent, 0);
Notification notif = new Notification(R.drawable.ic_launcher,"Please take your Looking 4 Answers daily survey now", System.currentTimeMillis());
notif.setLatestEventInfo(getApplicationContext(), from, message, contentIntent);
nm.notify(1, notif);
}
private void registerDevice(String registrationId) {
Log.d(Constants.TAG, "Will now register device with ID: " +
registrationId);
try {
registrar.registerIdWithC2DMService(registrationId);
} catch (RegistrationException e) {
Log.e(Constants.TAG, e.getMessage(), e);
}
}
private void handleRegistrationError(String error) {
Log.e(Constants.TAG, "Registration error " + error);
Log.e(Constants.TAG,
"Please click the registration button again to register the device.");
}
}
//RegistrationIDRegistrar.java files
class RegistrationIDRegistrar {
private static final String REGISTER_NEW_DEVICE = "register_device";
private static final String NODE_ID_PARAMETER = "nodeid";
private static final String REGISTRATION_IS_PARAMETER = "registrationid";
private final String url;
static final String MASHMOBILE_C2DM_SERVER_URL = "http://10.0.1.3:8888";
private RegistrationIDRegistrar(String url) {
this.url = url;
}
void registerIdWithC2DMService(final String registrationId) throws RegistrationException {
DefaultHttpClient httpClient = new DefaultHttpClient();
String nodeId = "realNodeId";
final String requestUrl = createRegistrationUrl(registrationId, nodeId);
HttpGet request = new HttpGet(requestUrl);
try {
HttpResponse response = httpClient.execute(request);
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode != 200) {
throw new RegistrationException(String.format(
"Could not register %s with the server.", registrationId),
requestUrl, statusCode);
}
} catch (IOException e) {
throw new RegistrationException(String.format(
"Registration of %s failed.", registrationId), requestUrl, e);
}
}
private String createRegistrationUrl(String registrationId, String nodeId) {
return String.format("%s/%s?%s=%s&%s=%s",
url, REGISTER_NEW_DEVICE, NODE_ID_PARAMETER, nodeId,
REGISTRATION_IS_PARAMETER, registrationId);
}
static RegistrationIDRegistrar getInstance() {
return new RegistrationIDRegistrar(MASHMOBILE_C2DM_SERVER_URL);
}
}
and at last use manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.technosoft.Akashc2dm"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<permission
android:name="com.google.android.c2dm.permission.C2D_MESSAGE"
android:protectionLevel="signature"/>
<uses-permission
android:name="com.google.android.c2dm.permission.C2D_MESSAGE"/>
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".Akashc2dmActivity"
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=".C2DMBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND"
>
<intent-filter>
<action
android:name="com.google.android.c2dm.intent.REGISTRATION"/>
<category android:name="com.technosoft.Akashc2dm"/>
</intent-filter>
<intent-filter>
<action
android:name="com.google.android.c2dm.intent.RECEIVE"/>
<category android:name="com.technosoft.Akashc2dm"/>
</intent-filter>
</receiver>
<service android:name=".RegistrationIDReceiver"
>
<intent-filter>
<action
android:name="com.technosoft.c2dm.intent.REGISTER"/>
<category android:name="com.technosoft.Akashc2dm"/>
</intent-filter>
</service>
<service
android:name=".C2DMService">
<intent-filter>
<action
android:name="com.technosoft.c2dm.intent.START_SERVICE"/>
<category android:name="com.technosoft.Akashc2dm"/>
</intent-filter>
</service>
<meta-data android:value="true" android:name="ADMOB_ALLOW_LOCATION_FOR_ADS"/>
</application>
please tell me how to fetch register id and send massage to third party appication in c2dm
C2DMBroadcastReceiver.java file
and then call
registerIdWithC2DMService(registrationId)
method fromRegistrationIDRegistrar
class.There has above code code working successfully.There has some change in C2DMBroadcastReceiver.java class for receive message and There has add MessageClass.java class for sending message to server
C2DMBroadcastReceiver.java file
MessageClass .java file
}
RegistrationIDRegistrar.java file