Copyright (c) 2008-2011 Vrije Universiteit, The Netherlands
All rights reserved.
Redistribution and use in source and binary forms,
with or without modification, are permitted provided
that the follo...
If you think the Android project interdroid-swan listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
package interdroid.swan.crossdevice;
//www.java2s.comimport java.io.IOException;
import android.util.Log;
import com.google.android.gcm.server.Constants;
import com.google.android.gcm.server.Message;
import com.google.android.gcm.server.Result;
import com.google.android.gcm.server.Sender;
publicclass Pusher {
publicstaticfinal String TAG = "SWAN Pusher";
publicstaticvoid push(String toRegistrationId, String expressionId,
String action, String data) {
push(null, toRegistrationId, expressionId, action, data);
}
publicstaticvoid push(final String fromRegistrationId,
final String toRegistrationId, final String expressionId,
final String action, final String data) {
new Thread() {
publicvoid run() {
Sender sender = new Sender(SwanGCMConstants.API_KEY);
Message.Builder builder = new Message.Builder();
builder.timeToLive(60 * 60).collapseKey("MAGIC_STRING")
.delayWhileIdle(true);
if (fromRegistrationId != null) {
// from is not allowed and results in InvalidDataKey, see:
// http://developer.android.com/google/gcm/gcm.html
builder.addData("source", fromRegistrationId);
}
builder.addData("action", action);
builder.addData("data", data);
builder.addData("id", expressionId);
Message message = builder.build();
try {
Result result = sender.send(message, toRegistrationId, 5);
if (result.getMessageId() != null) {
String canonicalRegId = result
.getCanonicalRegistrationId();
if (canonicalRegId != null) {
Log.d(TAG,
"same device has more than on registration ID: update database");
} else {
Log.d(TAG,
"successfully sent push message for id: "
+ expressionId + ", type: "
+ action + ", data: " + data);
}
} else {
String error = result.getErrorCodeName();
if (error.equals(Constants.ERROR_NOT_REGISTERED)) {
Log.d(TAG,
"application has been removed from device - unregister database");
} else {
Log.d(TAG, "no message id, error: " + error);
}
}
} catch (IOException e) {
Log.d(TAG,
"failed to deliver push message: " + e.toString());
}
}
}.start();
}
}