获取c2dm消息时出错

时间:2012-03-21 11:32:48

标签: android android-c2dm

我已经尝试过c2dm&的代码我已成功获得注册ID,但我没有收到来自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("dummy@google.com");  
    }  

    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>

2 个答案:

答案 0 :(得分:0)

尝试将空构造函数添加到RegisterActivity类,它应如下所示:

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

public RegisterActivity()
{

}

...

需要启动注册服务。

答案 1 :(得分: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);