0

I have 2 receivers and 2 GCMIntentService classes for GCM in my application; one inside my app and another one is included in a library that i have added to my application.When a message is received through GCM; I would want to some how identify which intentservice a received message is intented for and let the correct reciever handle it. Some one suggested here to propagate result to the next receiver if its not intended for mine but i couldnt manage to do it. I would really appreciate if some one could help me with that.

Community
  • 1
  • 1
Sayed Jalil Hassan
  • 2,535
  • 7
  • 30
  • 42
  • I wrote that suggestion. What exactly doesn't work for you? You can use the priority to determine which receiver will be called first, and you can use the sender id to know if the message is intended for a specific receiver. – Eran Oct 22 '13 at 14:16
  • Thanks for the response. Yes i could determine which receiver is it intended for but i dont know how to propagate result to the other reciever if its not intended for me. – Sayed Jalil Hassan Oct 22 '13 at 14:18
  • To identify messages for my reciever i am sending an identifier string with the message and inside my GCMIntentService onMessage() method i check if the received message contains that string. I am not sure if thats the right approach. Did you mean something like this ? if not , what is correct place to handle this. should i define custom GCMBroadcastReceiver ? – Sayed Jalil Hassan Oct 22 '13 at 14:33
  • If you don't define a custom GCMBroadcastReceiver then how do you expect to propagate the result? The default GCMBroadcastReceiver (of the now deprecated library) ends with `setResult(Activity.RESULT_OK, null /* data */, null /* extra */);`, I'm not sure about it, but setting the data and extras to null might prevent them from being passed to the next broadcast receiver. – Eran Oct 22 '13 at 14:41
  • Thanks Eran. I was using the deprecated Gcm api that wouldn't allow me overide methods of GCMBroadcastReceiver. I switched to the new GoogleCloudMessaging api and its working fine. both of them receive every message. But i still couldnt make it stop from propagating the message bundle to the other receiver once i handle it my self. I have tried both Result.canceled and result.ok. – Sayed Jalil Hassan Oct 25 '13 at 05:34
  • Perhaps you should try something like this - `setResult(Activity.RESULT_OK, null /* data */, null /* extra */);` - when you wish to stop from propagating the message. I'm not sure if it will work, but it's worth trying. – Eran Oct 25 '13 at 16:19

1 Answers1

0

Ok I have managed to solve it. Thanks to @Eran for his help. I was using the deprecated GCM api. The default implementation of GCMBroadcastReceiver had

    setResult(Activity.RESULT_OK, null /* data */, null /* extra */); 

in onReceive() method. This would prevent passing the result on to the next receiver. I tried to overide the onReceive method but it was final and wouldn't allow me overide it. So, i switched to the new GoogleCloudMessaging api and defined a custom Broadcast receiver and in its onReceive() metod i did this :

    @Override
    public void onReceive(Context context, Intent intent) {

        String message = intent.getExtras().getString("identifier_tag");

        // ignore if message not intended for us
        if (message == null) {
            setResultCode(Activity.RESULT_OK);
            return;
        }
        if (!message.equals(IDENTIFIER_TAG)) {
            setResultCode(Activity.RESULT_OK);
            return;
        }


        ComponentName comp = new ComponentName(context.getPackageName(),
                GcmIntentService.class.getName());
        // Start the service, keeping the device awake while it is
        // launching.
        startWakefulService(context, (intent.setComponent(comp)));

        // message has been handled; do not propagate
        setResult(Activity.RESULT_OK, null, null);

} 

What i did was to check if the received message was intended for me. If yes, i would invoke the intent service and setResult(Activity.RESULT_OK, null, null); would stop the message from being passed on to other receivers. If message wasn't intended for me, i would pass it to the next receiver. also in the manifest file, i set the priority for this receiver higher to make sure it receive the message first.

Sayed Jalil Hassan
  • 2,535
  • 7
  • 30
  • 42