C2DM不接收任何已发送的消息

时间:2011-11-25 10:24:38

标签: android android-c2dm

我有一个关于C2DM的问题,我昨天注册了,我收到了电子邮件,我的邮件和应用程序已获批准。从应用程序我得到registration_id,我有broadcast_receiver喜欢

@Override
public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();

    android.util.Log.d("REG_1","onReceive");


    if ("com.google.android.c2dm.intent.REGISTRATION".equals(action)) {
        final String registrationId = intent
                .getStringExtra("registration_id");
        String error = intent.getStringExtra("error");

        android.util.Log.d("REG_1",registrationId);

        SharedPreferences prefs = PreferenceManager
                .getDefaultSharedPreferences(context);
        String token = prefs.getString("token", null);
        String userId;
        try {
            userId = RestClient.getUserIdByToken(token).getString(
                    "user_id");

            Intent i = new Intent(context, RegService.class);
            i.putExtra("c2dm_registration_id",registrationId);
            i.putExtra("token", token);
            i.putExtra("user_id", userId);
            i.putExtra("device_id", "bla");
            i.setAction(android.content.Intent.ACTION_VIEW);
            context.startService(i);
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }



        saveRegistrationId(context, registrationId);
    }

我在注册期间输入此功能,我从命令行发送消息,如

curl https://www.google.com/accounts/ClientLogin -d Email=MYGOOGLEACCOUNT@gmail.com -d "Passwd=MY_PASSWORD" -d accountType=GOOGLE -d source=Google-cURL-Example -d service=ac2dm

curl --header "Authorization: GoogleLogin auth=DQAAAMMAAAC58D4X-5zjQFdYuGz7D9DhnuN4OUiz_gCtOJRSNwNLN0-wxveAEVL985hNKJXyQ_7U4sTfsUGh_3OXMLKpB5PNN1eaI4AfT19LaJ1vGJCZ_sSE0NDqGsC0mZVdMsYbE2Sz1r1WE_p5WNokfGMRdmxIHl0QCWb43lTD3iCvr51ujmnHnvpn2mDLWr6j9DtyDxADRw1to2iGgpJNelXmIA8tOzjyqF3szN-N2IYnihJ8H2t3G5wotOWy1EahB43Lv2NPdlV-A4yVSbdsYGM_AVdd" "https://android.apis.google.com/c2dm/send" -d registration_id=APA91bHhbsPedDVnYCaSJQMhWjfjK3W9jOaMgVITUHqw97w4fF_8fermSG22CzFvpPuTyRKnJFyJ_iwfgJEJ4uidURxuHZCCBuPtGAsv6NeVipmOd53Fkru_A3NW3cpIMo9gvuVxIB0QqxOvl1SmVfqRzD4qQfSNaw -d "data.test_result=This data will be send to your application as payload" -d collapse_key=2
id=0:1322216144957968%b3c4048a00000032

但它永远不会进入broadcatreceiver的onHandle函数。

    <receiver
        android:name="com.surveyce.android.c2dm.C2DMRegistrationReceiver"
        android:permission="com.google.android.c2dm.permission.SEND" >
        <intent-filter >
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <category android:name="com.surveyce.android" />
        </intent-filter>
        <!-- Receive the registration id -->
        <intent-filter >
            <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
            <category android:name="com.surveyce.android" />
        </intent-filter>
    </receiver>

...

    <uses-sdk android:minSdkVersion="8" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<!--<uses-permission android:name="android.permission.WAKE_LOCK" />-->
    <uses-permission android:name="com.surveyce.android.permission.C2D_MESSAGE" />
    <permission
        android:name="com.surveyce.android.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />
        <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-feature android:name="android.hardware.camera" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />

是否有人有类似的奇怪问题,这些问题会导致registrationId和发送消息,但是手机上没有收到消息?可能有什么问题,也许是因为我昨天注册了(但我怎么得到了该账号的rigistrationId和auth)?包名称和Gmail是帐户100%确定。

2 个答案:

答案 0 :(得分:1)

添加服务标签[在下面的服务标签中写下您的接收者名称,这是C2DMBaseReceiver的子类]

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

使用C2DMBroadcastReceiver而不是C2DMRegistrationReceiver

<receiver android:name="com.google.android.c2dm.C2DMBroadcastReceiver"
            android:permission="com.google.android.c2dm.permission.SEND">

答案 1 :(得分:0)

您可以参考以下链接以正确实施C2DM Google C2DM service