简体   繁体   中英

Using the GCM service to communicate between 2 applications

I'm trying to create 2 applications that communicate by the GCM service. Let's assume that I'm trying to sends string from app A to B and then from B to A.

I'm very new to the GCM service and I'm alittle bit confused. Every time you see myApiCode I replaced it in my original code with the api code.Here is the A code:

public class MainActivity extends Activity 
{
    private final String myApiKey = "903137756997";
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        GCMRegistrar.checkDevice(this);
        GCMRegistrar.checkManifest(this);
        final String regId = GCMRegistrar.getRegistrationId(this);
        if (regId.equals("")) {
          GCMRegistrar.register(this, "myApiCode");
        } else {
          Log.v("INFORMATION", "Already registered");
        }


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

}

And here is the GCMIntentService:

public class GCMIntentService extends GCMBaseIntentService
{
    private final String myApiKey = "903137756997";

    public GCMIntentService()
    {
        super("123");
    }
            ...
    @Override
    protected void onMessage(Context arg0, Intent arg1) 
    {
        Log.d("GCM", "RECIEVED A MESSAGE");
        System.out.println("123123123123");
    }
            ...

}

That code that I've attached would be app A and now I will attach the code of app B:

The following code is a service called from the main activity:

public void onCreate()
{
    super.onCreate();

    Sender sender = new Sender(myApiKey);
    Message message = new Message.Builder().build();
    Result result = null;
    try {
        result = sender.send(message, "123", 5);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    if (result.getMessageId() != null) {
         String canonicalRegId = result.getCanonicalRegistrationId();
         if (canonicalRegId != null) {
           // same device has more than on registration ID: update database
         }
        } else {
         String error = result.getErrorCodeName();
         if (error.equals(Constants.ERROR_NOT_REGISTERED)) {
           // application has been removed from device - unregister database
         }
        }
}

I have to mention that both of the apps are running without exceptions but looks like nothing happens.. I guess I have done something wrong with the keys because I still can't understand how app B will find app A.

You have to override the onRegistered method in GCMIntentService. This will be called when the GCM server returns a registration ID that was prompted by your call to GCMRegistrar.register.

Your implementation of this method should upload the String argument to a server you control, and the server can then send out messages targetting the ID you uploaded.

Also you should not push messages directly between apps this way, because it will require you to ship your private API key out in the app package in order to send messages.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM