0

I have setup the Android part for C2DM, and here's a part of the code:

private void handleRegistration(Context context, Intent intent) {
 // (...)

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

So it says to send the registration ID to my server. What should I do with it, and how can I send messages in php?

Code examples would be great, as I'm not very experienced with PHP. I've seen this answer, but that uses sessions, which I'm not using.

Community
  • 1
  • 1
nhaarman
  • 98,571
  • 55
  • 246
  • 278

1 Answers1

1

You need to save it to a database like MySQL so you will be able to access it later when you want to push to the device.

Then you'll send it somewhat like this

include("class.c2dm.php");
$c = new C2DM();
$c->setUsername('appemail@gmail.com');
$c->setPassword('emailpassword');
$c->setSource('com.company.app.package');

$c->setAuthCode($c->getAuthCode());

$regid = "longdeviceid"; //this is the device you want to push to. pull from your database.


$response = $c->send($regid, 'TRACK', array('action' => 'start_tracking'));

echo $response;

Get class.c2dm.php here: https://bitbucket.org/Dianoga/php-c2dm/src/0c299de8f10b/class.c2dm.php

bwoogie
  • 4,339
  • 12
  • 39
  • 72
  • So if I want to notify all the users I will have to call the `send` method `n` times? – nhaarman Feb 19 '12 at 16:06
  • Yup. And you have to do Exponential Backoff. So if Google says "Hold up I'm busy" you have to wait x amount of time before sending more. – bwoogie Feb 19 '12 at 20:04
  • One more thing: How can I use class.c2dm.php to send a custom message? I want to send the ID of the thing that changed with it, so the app can choose whether or not to fetch the data. – nhaarman Feb 19 '12 at 21:54
  • `$response = $c->send($regid, 'TRACK', array('action' => 'start_tracking'));` where it says TRACK you can change that to your ID and any extra stuff in the array 'action' => 'start_tracking' so maybe it would look like this: `$response = $c->send($regid, '1', array('action' => 'update_news_feed'));` – bwoogie Feb 19 '12 at 22:35