2012-03-21 67 views
0

我已經嘗試了c2dm的代碼&我已經成功獲得了註冊ID,但是我沒有從c2dm獲得消息,有人可以幫助我。獲取c2dm的消息時出錯

我的主類是

public class RegisterActivity extends Activity { 
    EditText text; 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
    } 

    public void register(View view) { 
     Log.e("Super", "Starting registration"); 
     Toast.makeText(this, "Starting", Toast.LENGTH_LONG).show(); 
     text = (EditText) findViewById(R.id.editText1); 
     C2DMessaging.register(this, text.getText().toString()); 
    } 
} 

我的註冊類

public class ResultActivity extends Activity { 
    TextView view; 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.result); 
     Bundle extras = getIntent().getExtras(); 
     if (extras != null) { 
      String string = extras.getString("message"); 
      view = (TextView) findViewById(R.id.c2dm); 
      view.setText(string); 
     } 
    } 
} 

我的接收機類是

public class C2DMReceiver extends C2DMBaseReceiver { 
    public C2DMReceiver() { 
     // Email address currently not used by the C2DM Messaging framework 
     super("[email protected]"); 
    } 

    public void onRegistered(Context context, String registrationId) 
      throws java.io.IOException { 
     Log.e("C2DM", "Registration ID received"); 
     Log.e("C2DM", registrationId); 
     Intent intent = new Intent(context, ResultActivity.class); 
     intent.putExtra("message", "Registration ID received"); 
     intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
     context.startActivity(intent); 
    }; 
    protected void onMessage(Context context, Intent intent) { 
      Log.e("C2DM", "Neue Message."); 
     Intent resultIntent = new Intent(context, ResultActivity.class); 
     resultIntent.putExtra("message", "Message received"); 
     resultIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
     context.startActivity(intent); 
    } 

    public void onError(Context context, String errorId) { 
     Log.e("C2DM", "Error occured!!!"); 
     Log.e("C2DM", errorId); 
    } 

} 

,這裏是我的清單文件

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.sample" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <permission 
     android:name="com.sample.permission.C2D_MESSAGE" 
     android:protectionLevel="signature" /> 

    <uses-permission android:name="com.sample.permission.C2D_MESSAGE" /> 
    <!-- Permissions --> 
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /> 
    <uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="android.permission.WAKE_LOCK" /> 

    <application 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" > 
     <activity 
      android:name="RegisterActivity" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 

     <service android:name=".C2DMReceiver" /> 

     <!-- 
       Only C2DM servers can send messages for the app. If permission is 
       not set - any other app can generate it 
     --> 
     <receiver 
      android:name="com.google.android.c2dm.C2DMBroadcastReceiver" 
      android:permission="com.google.android.c2dm.permission.SEND" > 

      <!-- Receive the actual message --> 
      <intent-filter> 
       <action android:name="com.google.android.c2dm.intent.RECEIVE" /> 

       <category android:name="com.sample" /> 
      </intent-filter> 
      <!-- Receive the registration id --> 
      <intent-filter> 
       <action android:name="com.google.android.c2dm.intent.REGISTRATION" /> 

       <category android:name="com.sample" /> 
      </intent-filter> 
     </receiver> 
     <activity android:name="ResultActivity" > 
     </activity> 
    </application> 

</manifest> 

回答

0

嘗試添加一個空的構造到RegisterActivity類,它應該是這樣的:

public class RegisterActivity extends Activity { 
    EditText text; 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
    } 

public RegisterActivity() 
{ 

} 

...

據以啓動註冊服務要求。

+0

我已經把它添加到我的代碼,但仍然沒有工作顯示異常從活動上下文之外調用startActivity()需要FLAG_ACTIVITY_NEW_TASK標誌。 – user1196969 2012-03-21 11:47:18

0
package Utils; 

import java.io.IOException; 
import java.io.OutputStream; 
import java.net.URL; 
import java.net.URLEncoder; 
import javax.net.ssl.HostnameVerifier; 
import javax.net.ssl.HttpsURLConnection; 
import javax.net.ssl.SSLSession; 
import android.util.Log; 

public class MessageUtil 
{ 
public final static String AUTH = "authentication"; 

public 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(); 
    if (responseCode == 200) 
    { 
     Log.d("error",conn.toString()); 
     Log.d("response",conn.getResponseMessage()); 
    } 
    return responseCode; 
} 

private static class CustomizedHostnameVerifier implements HostnameVerifier { 
    public boolean verify(String hostname, SSLSession session) { 
     return true; 
    } 
} 
} 
    //call the method from other activity 
    int responseCode = MessageUtil.sendMessage(token,registration_id, "your message for device"); 
     System.out.println(responseCode);