2012-06-26 124 views
1

可能重複:
Register the Android App with C2DM如何註冊應用C2DM

所以,我一直在尋找所有和閱讀的一切,我可以把我的手,以檢查如何與我的超酷聊天應用程序一起使用C2DM。

我看到有各種各樣的東西,我的服務器人必須照顧,但我沒有明白一件事。

註冊過程必須包含一個senderId,它基本上是一個Google ID,作爲使用該應用程序的應用程序(或用戶)轉移到Google服務器,這將您標識爲推送客戶端。

我的問題是,我必須用註冊對話框提示用戶嗎?這對我的用戶來說似乎是一件可怕的事情,因爲該應用程序已經使用Facebook連接,並提示太多對用戶不利,而且肯定會讓他們卸載該應用程序。

一個應用註冊C2DM的過程是什麼?以及如何使用Google Play使用的現有身份驗證令牌?


我讀(第三次)Vogella的turoial使用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", "[email protected]"); //WHICH EMAIL? 
    startService(intent); 
} 

是4號線的設備所有者的電子郵件或服務的電子郵件中使用的電子郵件?

如果我們正在談論用戶,有沒有一種方法來獲得這個沒有另一個身份驗證對話框?我已經有Facebook登錄,我不想讓用戶感到不適。

回答

0

首先你需要signup與谷歌的C2DM

其次得到驗證令牌的C2DM應用

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", "[email protected]"); 
    startService(intent); 
} 

該服務將與Google異步註冊,並將發送「com.google.android.c2dm.in帳篷.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; 
    } 

Reference

+0

這是** vogella博客文章**逐字。我看到它,並沒有回答任何東西 – thepoosh

+0

我剛纔提到的帖子,而不是給外部鏈接,這會更好。你需要什麼? –

+0

你已經找到答案了...很好 –