所以,我一直在寻找并阅读我可以用来检查如何在我的超酷聊天应用程序中使用C2DM的所有内容。
我看到我的服务器人员需要处理各种各样的事情,但我不明白一件事。
注册过程必须包含senderId,它基本上是作为使用该应用的应用(或用户)转移到Google服务器的Google ID,这会将您识别为推送客户端。
我的问题是,我是否必须通过注册对话框提示用户?这对我的用户来说似乎是一件可怕的事情,因为该应用已经使用Facebook连接并提示过多的那些对用户是敌对的,并且肯定会让他们卸载应用程序。
注册C2DM的一个应用程序的流程是什么?以及如何使用Google Play使用的现有身份验证令牌?
<小时/> 我(第三次)阅读了Vogella使用C2DM的语言,这是我的问题的基础:
public void register(View view) {
Intent intent = new Intent("com.google.android.c2dm.intent.REGISTER");
intent.putExtra("app",PendingIntent.getBroadcast(this, 0, new Intent(), 0));
intent.putExtra("sender", "youruser@gmail.com"); //WHICH EMAIL?
startService(intent);
}
是设备所有者的电子邮件或服务电子邮件在第4行使用的电子邮件?
如果我们谈论的是用户,有没有办法在没有其他身份验证对话框的情况下获取此用户?我已经有Facebook登录,我不想让用户感到不适。
答案 0 :(得分:1)
请参阅vogella的文章,非常简短的C2DM实施指南。
http://www.vogella.com/articles/AndroidCloudToDeviceMessaging/article.html
那里提到的所有东西以及代码,我个人实现它的代码,并且工作正常。
答案 1 :(得分:0)
首先,您需要signup使用谷歌的C2DM
第二次获得C2DM应用程序的Auth令牌
function(){
// Create the post data
// Requires a field with the email and the password
StringBuilder builder = new StringBuilder();
builder.append("Email=").append(email);
builder.append("&Passwd=").append(password);
builder.append("&accountType=GOOGLE");
builder.append("&source=MyLittleExample");
builder.append("&service=ac2dm");
// Setup the Http Post
byte[] data = builder.toString().getBytes();
URL url = new URL("https://www.google.com/accounts/ClientLogin");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setUseCaches(false);
con.setDoOutput(true);
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
con.setRequestProperty("Content-Length", Integer.toString(data.length));
// Issue the HTTP POST request
OutputStream output = con.getOutputStream();
output.write(data);
output.close();
// Read the response
BufferedReader reader = new BufferedReader(new InputStreamReader(con.getInputStream()));
String line = null;
String auth_key = null;
while ((line = reader.readLine()) != null) {
if (line.startsWith("Auth=")) {
auth_key = line.substring(5);
}
}
// Finally get the authentication token
// To something useful with it
return auth_key;
}
现在您需要将客户端移动设备注册到C2DM以接收更新
public void register(View view) {
Intent intent = new Intent("com.google.android.c2dm.intent.REGISTER");
intent.putExtra("app",PendingIntent.getBroadcast(this, 0, new Intent(), 0));
intent.putExtra("sender", "youruser@gmail.com");
startService(intent);
}
该服务将异步注册Google,并在成功注册后发送“ com.google.android.c2dm.intent.REGISTRATION ”意图。您的应用程序需要为此意图注册广播接收器。这还需要根据您的软件包使用权限,因为Android系统会在内部对其进行检查。
<receiver android:name=".C2DMMessageReceiver"
android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE"></action>
<category android:name="de.vogella.android.c2dm.simpleclient" />
</intent-filter>
</receiver>
//
public class C2DMRegistrationReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Log.w("C2DM", "Registration Receiver called");
if ("com.google.android.c2dm.intent.REGISTRATION".equals(action)) {
Log.w("C2DM", "Received registration ID");
final String registrationId = intent
.getStringExtra("registration_id");
String error = intent.getStringExtra("error");
Log.d("C2DM", "dmControl: registrationId = " + registrationId
+ ", error = " + error);
// Send and store this in your application server(unique for each device)
}
}
}
现在您可以开始通过服务器发送C2DM消息
private final static String AUTH = "authentication";
private static final String UPDATE_CLIENT_AUTH = "Update-Client-Auth";
public static final String PARAM_REGISTRATION_ID = "registration_id";
public static final String PARAM_DELAY_WHILE_IDLE = "delay_while_idle";
public static final String PARAM_COLLAPSE_KEY = "collapse_key";
private static final String UTF8 = "UTF-8";
public static int sendMessage(String auth_token, String registrationId,
String message) throws IOException {
StringBuilder postDataBuilder = new StringBuilder();
postDataBuilder.append(PARAM_REGISTRATION_ID).append("=")
.append(registrationId);
postDataBuilder.append("&").append(PARAM_COLLAPSE_KEY).append("=")
.append("0");
postDataBuilder.append("&").append("data.payload").append("=")
.append(URLEncoder.encode(message, UTF8));
byte[] postData = postDataBuilder.toString().getBytes(UTF8);
// Hit the dm URL.
URL url = new URL("https://android.clients.google.com/c2dm/send");
HttpsURLConnection
.setDefaultHostnameVerifier(new CustomizedHostnameVerifier());
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded;charset=UTF-8");
conn.setRequestProperty("Content-Length",
Integer.toString(postData.length));
conn.setRequestProperty("Authorization", "GoogleLogin auth="
+ auth_token);
OutputStream out = conn.getOutputStream();
out.write(postData);
out.close();
int responseCode = conn.getResponseCode();
return responseCode;
}