Android Cloud to Device Messaging

时间:2011-11-25 11:50:17

标签: android android-c2dm

我正在尝试使用Android Cloud to Device Messaging。我有注册应用程序的问题。获取错误com.google.process.gapps asyncFetch:无用户名。我在下面列出了我的代码

Intent intent = new Intent("com.google.android.c2dm.intent.REGISTER");
intent.putExtra("app",PendingIntent.getBroadcast(this, 0, new Intent(), 0));
intent.putExtra("sender", "xxx@gmail.com");
startService(intent);

我的注册/收件人:

package com.mpest;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;

public class MyC2dmReceiver extends BroadcastReceiver{

 private static String KEY = "c2dmPref";
    private static String REGISTRATION_KEY = "registrationKey";

    private Context context;
    @Override
    public void onReceive(Context context, Intent intent) {
        this.context = context;
            Log.i("aaa", "jnh");
        if (intent.getAction().equals("com.google.android.c2dm.intent.REGISTRATION")) {
            handleRegistration(context, intent);
        } 
        }
        private void handleRegistration(Context context, Intent intent) {
        String registration = intent.getStringExtra("registration_id");
        if (intent.getStringExtra("error") != null) {
            // Registration failed, should try again later.
            Log.d("c2dm", "registration failed");
            String error = intent.getStringExtra("error");
            if(error.equals("SERVICE_NOT_AVAILABLE")){
                Log.d("c2dm", "SERVICE_NOT_AVAILABLE");
            }else if(error.equals("ACCOUNT_MISSING")){
                Log.d("c2dm", "ACCOUNT_MISSING");
            }else if(error.equals("AUTHENTICATION_FAILED")){
                Log.d("c2dm", "AUTHENTICATION_FAILED");
            }else if(error.equals("TOO_MANY_REGISTRATIONS")){
                Log.d("c2dm", "TOO_MANY_REGISTRATIONS");
            }else if(error.equals("INVALID_SENDER")){
                Log.d("c2dm", "INVALID_SENDER");
            }else if(error.equals("PHONE_REGISTRATION_ERROR")){
                Log.d("c2dm", "PHONE_REGISTRATION_ERROR");
            }
            }
            else if (registration != null) {
            Log.d("c2dm", registration);
            //Editor editor =  context.getSharedPreferences(KEY, Context.MODE_PRIVATE).edit();
                // editor.putString(REGISTRATION_KEY, registration);
            //editor.commit();
           // Send the registration ID to the 3rd party site that is sending the messages.
           // This should be done in a separate thread.
           // When done, remember that all registration is done.
        }
    }

        private void handleMessage(Context context, Intent intent)
    {
            String mywish= intent.getStringExtra("wishes");    
              Toast.makeText(context,"my wishes : "+mywish,1).show(); 

        //Do whatever you want with the message
    }
}

我的表现

            <!-- Only C2DM servers can send messages for the app. If permission is not set - any other app can generate it --> 
  <receiver android:name=".C2DMReceiver" 
  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.mpest.MyC2dmReceiver" />
      </intent-filter>
      <!-- Receive the registration id -->
      <intent-filter>
          <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
          <category android:name="com.mpest.MyC2dmReceiver" />
      </intent-filter>
  </receiver>

2 个答案:

答案 0 :(得分:0)

在清单中添加一些权限

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />  
<permission android:name="com.mpest.permission.C2D_MESSAGE" android:protectionLevel="signature" />
<uses-permission android:name="com.mpest.permission.C2D_MESSAGE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />

答案 1 :(得分:0)

我猜您需要在模拟器上拥有一个有效且有效的Google帐户。 :)