处理GCM发送的意图时出错

时间:2012-09-11 13:07:11

标签: android google-cloud-messaging

当我处理GCM时,我在“”类上遇到了一些错误。 错误出现在这里:

onHandleIntent(Intent intent) 
  

“无法覆盖GCMBaseIntentService的最终方法”

handleRegistration 
  

“类型中的方法handleRegistration(Context,Intent)   GCMBaseIntentService不适用于参数(Intent)“

handleMessage  
  

“方法handleMessage(Intent)未定义类型   GCMIntenetService“

public final void onHandleIntent(Intent intent) {
            try {
                String action = intent.getAction();
                if (action.equals("com.google.android.c2dm.intent.REGISTRATION")) {
                    handleRegistration(intent);
                } else if (action.equals("com.google.android.c2dm.intent.RECEIVE")) {
                    handleMessage(intent);
                }
            } finally {
                synchronized (LOCK) {
                    sWakeLock.release();
                }
            }
        }

package com.example.elarabygroup;

import com.google.android.gcm.GCMBaseIntentService;
import android.content.Context;
import android.content.Intent;
import android.os.PowerManager;
import android.util.Log;

public class GCMIntenetService extends GCMBaseIntentService {
    public static String TAG = "GCMIntentService";
    private static PowerManager.WakeLock sWakeLock;
    private static final Object LOCK = GCMIntenetService.class;

    /*Handling Intents sent by GCM*/
    static void runIntentInService(Context context, Intent intent) {
        synchronized (LOCK) {
            if (sWakeLock == null) {
                PowerManager pm = (PowerManager) context
                        .getSystemService(Context.POWER_SERVICE);
                sWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
                        "my_wakelock");
            }
        }
        sWakeLock.acquire();
        intent.setClassName(context, GCMIntenetService.class.getName());
        context.startService(intent);
    }



    public GCMIntenetService(String senderId) {
        super(senderId);
        // TODO Auto-generated constructor stub
        Log.d(TAG, "[GCMIntentService] start - sender Id : " + senderId);
    }

    @Override
    protected void onError(Context arg0, String arg1) {
        Log.d("onError", arg1);
    }

    @Override
    protected boolean onRecoverableError(Context context, String errorId) {
        Log.d("onRecoverableError", errorId);
        return false;
    }

    @Override
    /*
     * protected void onMessage(Context arg0, Intent arg1) { Log.d("onMessage",
     * String.valueOf(arg1)); }
     */
    protected void onMessage(Context arg0, Intent arg1) {

        Log.d("GCM", "RECIEVED A MESSAGE");
        // Get the data from intent and send to notificaion bar
        generateNotification(arg0, arg1.getStringExtra("**notificaion**"));
    }

    private void generateNotification(Context arg0, String stringExtra) {
        // TODO Auto-generated method stub

    }

    @Override
    protected void onRegistered(intent) {

    try {
        String action = intent.getAction();
        if (action.equals("com.google.android.c2dm.intent.REGISTRATION")) {
            handleRegistration(intent);
        } else if (action.equals("com.google.android.c2dm.intent.RECEIVE")) {
            handleMessage(intent);
        }
    } finally {
        synchronized (LOCK) {
            sWakeLock.release();
        }
    }

}

    @Override
    protected void onUnregistered(Context arg0, String arg1) {
        Log.d("onUnregistered", arg1);
    }
}

2 个答案:

答案 0 :(得分:1)

您不应该覆盖GCMBaseIntentService的最终方法。你只需要ovveride这个基类的回调方法。这是一个例子:

public class GCMIntentService extends GCMBaseIntentService {

    private static final String GCM_SENDER_ID = "your_sender_id";

    public GCMIntentService() {
        super();
    }

    @Override
    protected void onRegistered(Context context, String gcmDeviceToken) {
        // remember and save somewhere "gcmDeviceToken"
    }

    @Override
    protected void onUnregistered(Context context, String s) {
        // Push unregistered processing
    }

    @Override
    protected void onError(Context context, String errorId) {
        // push error processing
    }

    @Override
    protected void onMessage(Context context, Intent intent) {
        // process Push message
    }

    public static void registerInGCMService(Context context) {
        if (!checkIsGCMServiceAvailable(context)) {
            return;
        }
        final String regId = GCMRegistrar.getRegistrationId(context);
        if (regId.equals("")) {
            try {
                GCMRegistrar.register(context, GCM_SENDER_ID);
            } catch (Exception ex) {
            }
        } else {
            // Already registered
        }
    }

    public static boolean checkIsGCMServiceAvailable(Context context) {
        try {
            GCMRegistrar.checkDevice(context);
            GCMRegistrar.checkManifest(context);
            return true;
        } catch (Throwable th) {
            return false;
        }
    }

}

更新:请注意 - 您应该使用自己的值更改 GCM_SENDER_ID 常量,它应该是数值,例如“1234567890123”

答案 1 :(得分:0)

@Override
    protected void onRegistered(Context context, String registrationId) {
        Log.i(TAG, "Device registered: regId = " + registrationId);
        GCMRegistrar.setRegisteredOnServer(context, true);
    }

    @Override
    protected void onMessage(Context context, Intent intent) {
        Log.i(TAG, "Received message");
        Log.i(TAG, "EXTRAS" + intent.getExtras());
        String message = getString(R.string.gcm_message);
        // notifies user about message

    }

    @Override
    protected void onUnregistered(Context context, String registrationId) {
        Log.i(TAG, "Device unregistered");
        if (GCMRegistrar.isRegisteredOnServer(context)) {
                Log.i(TAG, "unregistering device (regId = " + regId + ")");
        GCMRegistrar.setRegisteredOnServer(context, false);
        } else {
            // This callback results from the call to unregister made on
            // ServerUtilities when the registration to the server failed.
            Log.i(TAG, "Ignoring unregister callback");
        }
    }

请参阅GCM google教程http://developer.android.com/guide/google/gcm/gs.html