egydeveloper
egydeveloper

Reputation: 585

error when handling intents sent by GCM

I had some errors on "" class when I was handling the GCM . the error appeared here :

onHandleIntent(Intent intent) 

"Cannot override the final method from GCMBaseIntentService"

handleRegistration 

"The method handleRegistration(Context, Intent) in the type GCMBaseIntentService is not applicable for the arguments (Intent)"

handleMessage  

"The method handleMessage(Intent) is undefined for the type 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();
                }
            }
        }

class

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);
    }
}

Upvotes: 2

Views: 1653

Answers (2)

HitOdessit
HitOdessit

Reputation: 7266

You shouldn't override final methods of GCMBaseIntentService. You need ovveride only callback methods of this base class. Here is an example:

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;
        }
    }

}

UPDATE: please note - you should change value of GCM_SENDER_ID constant with your own, it should be numeric value, something like "1234567890123"

Upvotes: 1

Yahor10
Yahor10

Reputation: 2132

@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");
        }
    }

See GCM google tutorial http://developer.android.com/guide/google/gcm/gs.html

Upvotes: 0

Related Questions