Re: GCM returns "Unavailable"

1,320 views
Skip to first unread message

Monnand

unread,
Jun 29, 2012, 6:53:54 PM6/29/12
to androi...@googlegroups.com, andro...@googlegroups.com
I think it would be better to cc to android-gcm group. I have already corrected the code --- It seems like a bug on GCM side.

Here is the message I sent causing "unavailable error"
{"registration_ids":["SOME REG ID"],"collapse_key":"c058391ee54cec4b8812ab121411afbaf6de024a","data":{"msg":"Hello World"},"delay_while_idle":false,"time_to_live":4294967295}

Here is the message I sent without problem:
{"registration_ids":["SOME REG ID"],"data":{"msg":"Hello World"}}

Then I thought it might be the value of time_to_live field is too large. So I tried this:

{"registration_ids":["APA91bHaWwM9ytxfHVBjHZTtQ7Ggw_rovTOYFMzhi5ae-gLySoejokMpBx9krhynlYj_9kG2d3rg7C5carlybQcfIiIlIho4MR1AjWJIhk45SJ4mMFWP038KUP5THTZtVsuAPicio54IRqjSMgCNy9TgOdZAL_toow"],"collapse_key":"67ab7e1dae275e389c9fbc323b68a2b568962294","data":{"msg":"Hello World"},"time_to_live":4294967295}

And I got unavailable; but the following data could be sent.

 {"registration_ids":["APA91bHaWwM9ytxfHVBjHZTtQ7Ggw_rovTOYFMzhi5ae-gLySoejokMpBx9krhynlYj_9kG2d3rg7C5carlybQcfIiIlIho4MR1AjWJIhk45SJ4mMFWP038KUP5THTZtVsuAPicio54IRqjSMgCNy9TgOdZAL_toow"],"collapse_key":"1c2ded740aca18f2148fec0a78e9a1a57526440e","data":{"msg":"Hello World"},"time_to_live":120}

Would you please check if I am correct? It might be better if set time_to_live to the default value if the value in the request is too large.

Regards,
-Monnand




On Friday, June 29, 2012 6:36:29 PM UTC-4, Monnand wrote:
Hi Felipe,

I got it every time I retried. I am using my own code. Is it possible that I got unavailable error because of the error on my side? (According to documents, it should not be possible.)

Here is the result I received:

{"multicast_id":5030400576422639801,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"Unavailable"}]}

Thank you!

Regards,
-Monnand

On Friday, June 29, 2012 6:04:52 PM UTC-4, Felipe Leme wrote:
Did you get it just once or did you get it again when you retried?

Usually, you get an 'Unavailable' when the server could not process one message in the multicast, in which case you should try again. But if you're always getting that error, it might be caused by something else that we would have to investigate.

Notice that if you use the Sender class from the gcm-server.jar, it has logic to automatically retry.



On Fri, Jun 29, 2012 at 1:33 AM, sparkgene <jun...@gmail.com> wrote:
I'm working on migrate to gcm.

Using gcm.jar to get the registration id is successfully done.
Sending the message to gcm endpoint returns HTTP Status 200.

But the result contains error => Unavailable.

In the GCM document(http://developer.android.com/guide/google/gcm/gcm.html) this error solution is "If it is Unavailable, you could retry to send it in another request."

Do anyone have information about this error?



Monnand

unread,
Jun 29, 2012, 6:57:18 PM6/29/12
to androi...@googlegroups.com, andro...@googlegroups.com
Oops. I forgot to remove my registration ID. Anyway, it just a test device with nothing valuable data. Please ignore it.

Sorry about that.

Regards,
-Monnand

Francesco Nerieri

unread,
Jun 29, 2012, 7:53:52 PM6/29/12
to andro...@googlegroups.com, androi...@googlegroups.com
Monnand,

we do set the value to 4 weeks if the number is bigger than that,
however if the number is too big (you can write all the digits you
like) than we do invalidate the request.

We definitely need to return a better error code for this case and we
are already working on it. We will also update the documentation
accordingly.

Thanks for pointing this out,
 f

Monnand

unread,
Jun 29, 2012, 11:21:28 PM6/29/12
to andro...@googlegroups.com, androi...@googlegroups.com
Hi Francesco,

Thanks for your reply. I can see all you guys are working hard on this project. Really appreciated.

As you mentioned, it might be better to return another error code on this situation. I think I was lost in this document before.

Regards.
-Monnand

Jun Ichikawa

unread,
Jun 30, 2012, 11:43:15 AM6/30/12
to andro...@googlegroups.com, androi...@googlegroups.com
Thanks for the reply.

I think the problem is resolved in this.

Regards
Jun


2012/6/30 Monnand <mon...@gmail.com>:
> --
> You received this message because you are subscribed to the Google Groups
> "android-gcm" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/android-gcm/-/0s3OLbrAzFMJ.
>
> To post to this group, send email to andro...@googlegroups.com.
> To unsubscribe from this group, send email to
> android-gcm...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/android-gcm?hl=en.

Manu

unread,
Jul 3, 2012, 2:04:19 PM7/3/12
to andro...@googlegroups.com, androi...@googlegroups.com
This is still not working.
As much as C2DM works great, GCM seems to be rather half baked. The documentation contains invalid JSON samples.
The unavailable error is persistent.

Given this (I partially masked the ID FWIW):
data: {"registration_ids":["APA91xxxxxxxxxxxxxxxkYLsifHHxHoFi6_agmLJ5f_v0HaDHKnVOjWRPUDdNRaDOjUbzMCJQB5xI30VPy9s04BObj3ZAp8iPGCpd2MuISBuZi8ewKIMux7SoRp3xxxxxxxxxxxxxxxx"],"collapse_key":"1234567890","data":{"type":"CR","msg":"a7ee817a-b313-11e1-b622-4bfd167404b6"}}

I get those:

{"multicast_id":7788034861060991441,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"Unavailable"}]}
{"multicast_id":7788034861060991441,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"Unavailable"}]}

I could make a small loop to retry 50 times and I would get the exact same message every single time.
If there is a problem in the JSON request, the error message should be change to be a little more helpful. I have tried with and without: collapse_key, time_to_live but to no avail.

If I send this in a x-www-form-urlencoded string format, with the minimal amount of stuff (id + message) I get an 'unauthorized', even though the ID is the same that I pass using JSON.

Hope this helps.

Francesco

unread,
Jul 3, 2012, 2:38:54 PM7/3/12
to andro...@googlegroups.com, androi...@googlegroups.com
If you get Unavailable in this case it means you are sending a bad JSON request.
We are working on providing a better error code for this, stay tuned.

Hth,
f
> --
> You received this message because you are subscribed to the Google Groups
> "android-gcm" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/android-gcm/-/MJuSNBdTfdgJ.

Manu

unread,
Jul 4, 2012, 2:24:20 AM7/4/12
to andro...@googlegroups.com, androi...@googlegroups.com
Hi there,

Seems like the error handling is totally out of whack.

According to the doc, JSON formatting/syntax errors should get you a HTTP 400 but so far, I get different behaviors from the GCM server:

For some errors in the JSON data, GCM returns a 200 with 'unavailable'
In some other cases, like when I send the exact same request but I add "time_to_live": 120, then I get a 401...

The JSON is created with the json Python module and the json.dumps(...) function so at least I know that it is formatted properly. I verified the keywords over and over and they are correct (since I usually only use: registration_ids and data, it keeps it simple).

The Registration ID that I use was returned by the GCM server using the project ID as sender ID, which I reckon is the proper way of doing it. The Server Key (Simple API Access Key) was generated inside that project and is sent verbatim with the Authorization:key=<SERVER_KEY>.

Regards


On Tuesday, July 3, 2012 11:38:54 AM UTC-7, Francesco Nerieri wrote:
If you get Unavailable in this case it means you are sending a bad JSON request.
We are working on providing a better error code for this, stay tuned.

Hth,
  f

JonathanC

unread,
Jul 4, 2012, 3:50:02 AM7/4/12
to andro...@googlegroups.com, androi...@googlegroups.com
Hey guys,

I'm experiencing similar issues:

2012-07-04 09:45:04+0200 [-] Sending: {"data": {"message": ["c\u00e6mera 7318952 - is nu online en klaar voor gebruik"]}, "registration_ids": ["<mykey>"]}
2012-07-04 09:45:04+0200 [-] AndroidNotificationSender:send:120 Sent.
2012-07-04 09:45:04+0200 [HTTP11ClientProtocol (TLSMemoryBIOProtocol),client] [Droid] Message transmit response:
2012-07-04 09:45:04+0200 [HTTP11ClientProtocol (TLSMemoryBIOProtocol),client] {"multicast_id":7938595614873647090,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"Unavailable"}]}
2012-07-04 09:45:04+0200 [HTTP11ClientProtocol (TLSMemoryBIOProtocol),client] Finished  
2012-07-04 09:45:04+0200 [HTTP11ClientProtocol (TLSMemoryBIOProtocol),client] [Droid] Message transmit response:
2012-07-04 09:45:04+0200 [HTTP11ClientProtocol (TLSMemoryBIOProtocol),client] {"multicast_id":7614338315620730041,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"Unavailable"}]}
2012-07-04 09:45:04+0200 [HTTP11ClientProtocol (TLSMemoryBIOProtocol),client] Finished 

Did I encode the data incorrectly?

Friendly greetings,
    Jonathan

JonathanC

unread,
Jul 4, 2012, 4:05:59 AM7/4/12
to andro...@googlegroups.com, androi...@googlegroups.com
After making the encoded json data somewhat closer to the sample on http://developer.android.com/guide/google/gcm/adv.html :

{"delay_while_idle": true, "collapse_key": "Event", "time_to_live": 86400, "registration_ids": ["<mykey>"], "data": {"message": "Testing"}}

I'm still getting 'Unavailable' ... any news on this issue? :)

Steno

unread,
Jul 4, 2012, 12:07:33 PM7/4/12
to andro...@googlegroups.com, androi...@googlegroups.com
Hi all,
I'm trying to send a notification from my php server to an android application.
I've found some php code to do this, but i still have "Unavailable " error when i try to send the message.

$apiKey= "AIzaS...my browser api key of application";
$registrationIDs = array("registration id of the device");
// Message to be sent
$message = "Test";

// Set POST variables

$fields = array(
                'registration_ids'  => $registrationIDs,
                'data'              => array( "message" => $message ),
                );

$headers = array( 
                    'Authorization: key=' . $apiKey,
                    'Content-Type: application/json'
                );

Can you help me?

Thanks, 
Steno.

Manu

unread,
Jul 4, 2012, 1:17:44 PM7/4/12
to andro...@googlegroups.com, androi...@googlegroups.com
Somehow I managed to get it to work.
What I did temporarily was to use a key for browser apps. That didn't work so I removed the referrers. Still didn't work. 
Went back to use the Key for server apps but I removed the IP restriction. Still got the 'unavailable'
I added a "time_to_live": 120 and I got a 400 Bad Request. 
Replaced the "time_to_live" by "delay_while_idle": false and it successfully sent the message.
I put the IP restriction back (xxx.xxx.xxx.xxx/27) and it still works. I have no idea as to why it works now but I don't feel very confident about this. Seems to me that it can break without warning at pretty much any time.

Hopefully more updates later.

Steno

unread,
Jul 4, 2012, 3:51:17 PM7/4/12
to andro...@googlegroups.com, androi...@googlegroups.com
Thanks for your reply.
Sorry but I'm just starting out with GCM and I don't understand very well how I can put the delay_while_idle or time_to_live flags using PHP.
Is necessary to use the server key or it works also with a browser key?

francesco nerieri

unread,
Jul 4, 2012, 4:24:34 PM7/4/12
to andro...@googlegroups.com
inline...

> data:
> {"registration_ids":["APA91xxxxxxxxxxxxxxxkYLsifHHxHoFi6_agmLJ5f_v0HaDHKnVOjWRPUDdNRaDOjUbzMCJQB5xI30VPy9s04BObj3ZAp8iPGCpd2MuISBuZi8ewKIMux7SoRp3xxxxxxxxxxxxxxxx"],"collapse_key":"1234567890","data":{"type":"CR","msg":"a7ee817a-b313-11e1-b622-4bfd167404b6"}}
>
if the top line "data:" is part of the request, that is what is wrong.
f

---
https://plus.google.com/u/0/104524825852741167674

francesco nerieri

unread,
Jul 4, 2012, 4:30:33 PM7/4/12
to andro...@googlegroups.com
In general:
if you get 200 Unavailable it means that the JSON is not formatted
correctly (we are working to fix this and return a 400 instead with a
better error code, sorry for the confusion here)
If you get a 400, it means the request was wrong (JSON parsed fine but
some parameters are wrong)

This has nothing to do with authentication. Only if you get a 401 it
means you have issues with Auth.

Since the API key is part of the JSON request you might be getting a
200 Unavailable (because we can't parse your request).
If after this happens, you fix the request and GCM can parse it, you
could get a 401 (if auth is wrong), since now we are able to validate
your API key (which happens after the parsing).

Hth,
f

---
https://plus.google.com/u/0/104524825852741167674

Manu

unread,
Jul 4, 2012, 10:55:37 PM7/4/12
to andro...@googlegroups.com
Thanks.

The [data:] wasn't part of the payload, I just forgot to remove it before I posted.
At this point, I got the service to send my messages. I haven't changed the JSON content besides removing the TTL and playing around with the server keys.
I removed the IP/domain restrictions and put them back in after it started working properly. I still don't understand what was going on but the messages are going through now. 

Manu

unread,
Jul 4, 2012, 11:03:35 PM7/4/12
to andro...@googlegroups.com, androi...@googlegroups.com
Steno,

Sadly I don't quite understand what the deal is with the server key versus browser key. I was searching for answers and I found something on stack overflow (http://stackoverflow.com/questions/11242743/gcm-with-php-google-cloud-messaging).
That particular thread has a bunch of PHP code that you could look at.

My server side scripts are Python and not PHP and clearly, I'm not a PHP expert but my guess is that for you to add some contents to your JSON query, you'd have to do something like this:

$data = array(
       
'registration_ids' => array($reg),
        'delay_while_idle' => 'false',
       
'data' => array('msg' => 'hello world')
);

Basically, you had whatever you need to the array. The call to json_encode($data) will take care of the formatting.
Hope this helps!

jerem

unread,
Jul 5, 2012, 5:17:40 AM7/5/12
to andro...@googlegroups.com, androi...@googlegroups.com
I think it won't work with quotes around boolean >'false'<, you should try : >false< or >0<

Jeremy

JonathanC

unread,
Jul 5, 2012, 9:14:24 AM7/5/12
to andro...@googlegroups.com, androi...@googlegroups.com
I resolved this, obviously you also receive 'Unavailable' if an app hasn't registered with a senderid yet.

Woot, a working python gcm sender :-)

Steno

unread,
Jul 5, 2012, 10:26:01 AM7/5/12
to andro...@googlegroups.com, androi...@googlegroups.com
Thank you all for replies,
I tried with this code but i still have the same problem.

$apiKey= 'AIzxxxxxxxxxxxxxxxxlSpno'; // my API Key for browser
$registrationIDs = array("APxxxxxxxxxxxxxxxxxxxxdCj2");  // registration ID of my device
$message = 'Hello';
$data = array(
                'registration_ids' => $registrationIDs,
        'delay_while_idle' => false,
                'data'             => array( 'msg' => $message )
 );

$headers = array( 
                'Authorization: key=' . $apiKey,
                'Content-Type: application/json'
 );

$ch = curl_init(); // Open connection

// Set the url, number of POST vars, POST data
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $data ));

$result = curl_exec($ch);
// Close connection
curl_close($ch);
echo $result;

The result on browser is:
{"multicast_id":7419186489615074124,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"Unavailable"}]} 

Someone understands what has gone wrong?

Thanks,
Steno.

Steno

unread,
Jul 5, 2012, 11:24:18 AM7/5/12
to andro...@googlegroups.com, androi...@googlegroups.com
I tried with this code:

$data = array(
                'registration_ids' => $registrationIDs,
'collapse_key' => "Test-key",
        'time_to_live' => 3600,
        'delay_while_idle' => false,
                'data'             => array( 'msg' => $message )
);

and now it works!
Now i'm trying to retrieve data sent by the gcm notification from my application code. I hope to do it easily.
Thank you all for help!
Steno

Manu

unread,
Jul 5, 2012, 12:37:34 PM7/5/12
to andro...@googlegroups.com, androi...@googlegroups.com
Steno,

I'm glad you got it to work.
Now you've done the hardest part. For your app, you can use the GCM class helper the docs talk about (although, it won't appear in the list of extras for me) or you can code your own receiver, which is trivial.

I know this is off topic but here's what you need:

--- in your AndroidManifest.xml:

    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.GET_ACCOUNTS"/>
    <uses-permission android:name="android.permission.WAKE_LOCK"/>
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    <permission android:name="yourpackage.C2D_MESSAGE" android:protectionLevel="signature" />
    <uses-permission android:name="yourpackage.C2D_MESSAGE"/>

// Replace yourpackage by your own package name.

--- In your service 

public class MyService extends Service {
        public static String BROADCAST_ACTION = "yourpackage.YourActivity.GCMALERT";
...
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
IntentFilter filter = new IntentFilter();
        filter.addAction(MyService.BROADCAST_ACTION);
        registerReceiver(receiver, filter);
}
@Override
public void onDestroy() {
unregisterReceiver(receiver);
super.onDestroy();
}

private BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
         Bundle extras = intent.getExtras();
        String type = extras.getString("xxxx"); // this is what is in your "data" : { ... }
        String msg = extras.getString("yyyy"); // this is what is in your "data" : { ... }
       
                // Create notification here or do whatever you need
        }
    };
}

--- Receiver (which you need to have declared in your AndroidMAnifest.xml as per the documentation. The receiver is what does all the work.

public class GCMMessageReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Log.w("GCM", "Message Receiver called");
if ("com.google.android.c2dm.intent.RECEIVE".equals(action)) {
Log.w("GCM", "Received message");
final String xxxx = intent.getStringExtra("xxxx");
final String yyyy = intent.getStringExtra("yyy");
Log.d("GCM", "dmControl: xxxx = " + xxxx);
Log.d("GCM", "dmControl: yyyy = " + yyyy);
if (xxxx.equals("something")) {  // sends a notification to your service
Intent broadcast = new Intent();
broadcast.putExtra("yyyy", yyyy);
broadcast.putExtra("xxxx", xxxx);
broadcast.setAction(MyService.BROADCAST_ACTION);
context.sendBroadcast(broadcast);
return;
}
}
}
}


Of course this is just a rough draft of what you could do and you can do more complicated things but that should give you an idea.

Felipe Leme

unread,
Jul 5, 2012, 2:02:08 PM7/5/12
to andro...@googlegroups.com
Did you upgrade the Android SDK Platform Tools to release 20? If you did and GCM is still not showing, try to clear the SDK cache and restart it.

Manu

unread,
Jul 5, 2012, 2:04:21 PM7/5/12
to andro...@googlegroups.com
Will do, thanks!


On Thursday, July 5, 2012 11:02:08 AM UTC-7, Felipe Leme wrote:
Did you upgrade the Android SDK Platform Tools to release 20? If you did and GCM is still not showing, try to clear the SDK cache and restart it.

Steno

unread,
Jul 5, 2012, 3:28:48 PM7/5/12
to andro...@googlegroups.com, androi...@googlegroups.com
Thank you very much Manu,
it works perfectly!  I hope not to find other problems.

Steno

Javi

unread,
Jul 7, 2012, 9:53:05 AM7/7/12
to andro...@googlegroups.com, androi...@googlegroups.com
Hi Steno,

What kind of API key did you add in your account? Key for key browser? Which referers? Did you fill anything?

I'm getting crazy with this. If I use the browser key I added at the beginning I get MissmatchSenderID. If I use a new one with no referrers I get Unavailable. No matter how many times I try I get the same Error.

If I change the API Key it doesn't change anything, so I guess it's like Francesco said, first of all is the json parser and then the Authorization key. But if you don't send that header you will be kick out before.

How can Google release this change making it officially, deprecating the old method, with no documentation at all, just "If you receive a Unavailable, it means your json is not well-formed". Is there any page where we can check the corrected structured of the json????

Thank you in advance.

Steno

unread,
Jul 9, 2012, 11:53:19 AM7/9/12
to andro...@googlegroups.com, androi...@googlegroups.com
Hi Javi,
sorry if I answer only now.
I used a "Key for browser apps (with referers)" with "Any referer allowed".
Php code that I used is what I posted above, unfortunately I do not know what else tell you to help.

Steno

JonathanC

unread,
Jul 24, 2012, 2:02:34 PM7/24/12
to androi...@googlegroups.com, andro...@googlegroups.com
Hey Christian,


Grtz

On Thursday, July 5, 2012 5:16:17 PM UTC+2, Christian Carnero wrote:
Hi JonathanC!
Im trying to make this work but I always receive the "200 + ´Unavailable´" message 

I read this and I want to know what do you mean when you mention "if an app hasn't registered with a senderid yet."?

regards
Christian
Reply all
Reply to author
Forward
0 new messages