99

Update: GCM is deprecated, use FCM

I am implementing Google Cloud Messaging in my application. Server code is not ready yet and in my environment due to some firewall restrictions I can not deploy a test sever for push notification. What I am looking for is a online server which would send some test notifications to my device to test my client implementation.

Adnan
  • 5,025
  • 5
  • 25
  • 44
  • You did delete other post thats why im writing here :-) NotificationListenerService was added in api 18... Just store ids in SharedPreferences as int array and do some logic to chceck size of array if after adding new id is bigger than you need take first element and cancel... – Selvin Mar 07 '14 at 17:07
  • 1
    you can test using http://www.pushtry.com – Arvind Aug 19 '16 at 21:30

4 Answers4

169

Found a very easy way to do this.

Open http://phpfiddle.org/

Paste following php script in box. In php script set API_ACCESS_KEY, set device ids separated by coma.

Press F9 or click Run.

Have fun ;)

<?php


// API access key from Google API's Console
define( 'API_ACCESS_KEY', 'YOUR-API-ACCESS-KEY-GOES-HERE' );


$registrationIds = array("YOUR DEVICE IDS WILL GO HERE" );

// prep the bundle
$msg = array
(
    'message'       => 'here is a message. message',
    'title'         => 'This is a title. title',
    'subtitle'      => 'This is a subtitle. subtitle',
    'tickerText'    => 'Ticker text here...Ticker text here...Ticker text here',
    'vibrate'   => 1,
    'sound'     => 1
);

$fields = array
(
    'registration_ids'  => $registrationIds,
    'data'              => $msg
);

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

$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, 'https://android.googleapis.com/gcm/send' );
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( $fields ) );
$result = curl_exec($ch );
curl_close( $ch );

echo $result;
?>

For FCM, google url would be: https://fcm.googleapis.com/fcm/send

For FCM v1 google url would be: https://fcm.googleapis.com/v1/projects/YOUR_GOOGLE_CONSOLE_PROJECT_ID/messages:send

Note: While creating API Access Key on google developer console, you have to use 0.0.0.0/0 as ip address. (For testing purpose).

In case of receiving invalid Registration response from GCM server, please cross check the validity of your device token. You may check the validity of your device token using following url:

https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=YOUR_DEVICE_TOKEN

Some response codes:

Following is the description of some response codes you may receive from server.

{ "message_id": "XXXX" } - success
{ "message_id": "XXXX", "registration_id": "XXXX" } - success, device registration id has been changed mainly due to app re-install
{ "error": "Unavailable" } - Server not available, resend the message
{ "error": "InvalidRegistration" } - Invalid device registration Id 
{ "error": "NotRegistered"} - Application was uninstalled from the device
Adnan
  • 5,025
  • 5
  • 25
  • 44
  • The API access key is specific to the ip of server(generated from google console). If not we can do the API call from any server – guy_fawkes Mar 04 '14 at 14:22
  • 1
    Not really :) For testing purposes, you can use 0.0.0.0/0 ip. it would work. – Adnan Mar 04 '14 at 14:29
  • 4
    I get every time (with api key and device id: error":"InvalidRegistration – Leandro Bardelli Aug 14 '14 at 03:29
  • Some issue here always I am getting error InvalidRegistration, can someone help. – Kamleshwar Dec 01 '14 at 22:04
  • kamleshwar, are you putting your device id here:array("YOUR DEVICE IDS WILL GO HERE" ); -- if yes perhaps you are adding some wrong device id – Adnan Dec 01 '14 at 22:09
  • @kamleshwar "InvalidRegistration" means that your device ID is no good. Make sure you validate it Ex: check against `https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=1/$token` before storing it. – Chris Dec 18 '14 at 17:13
  • 4
    Please regenerate `API KEY` after, as it is not too safe to give them your key. – Flash Thunder Jan 16 '15 at 17:16
  • I'm getting [{"error":"NotRegistered"}]}. It was working just fine but suddenly it is showing this. HELP! – Tushar Gogna Feb 10 '15 at 11:40
  • Tushar, You application has been uninstalled. – Adnan Feb 11 '15 at 14:49
  • 6
    i keep on getting Unauthorized Error 401. any idea why? – Razel Soco Sep 10 '15 at 07:55
  • 2
    { "error": "invalid_token", "error_description": "Invalid Value" } i found this. and getting invalid reg id. – Anand Oct 20 '15 at 11:58
  • 1
    For anyone wondering, the registrationID corresponds to the deviceToken with Parse (and likely some other frameworks). The API_ACCESS_KEY was the server key generated in the Google API Console. Interestingly enough, the tokenInfo request still came back with InvalidValue even when the values I used created a successful request. – zgc7009 Aug 15 '16 at 16:28
  • it says **The request's Authentification (Server-) Key contained an invalid or malformed FCM-Token (a.k.a. IID-Token). Error 401** i double check all key they are fine and working good with postman – Sohan Dec 24 '16 at 10:53
159

POSTMAN : A google chrome extension

Use postman to send message instead of server. Postman settings are as follows :

Request Type: POST

URL: https://android.googleapis.com/gcm/send

Header
  Authorization  : key=your key //Google API KEY
  Content-Type : application/json

JSON (raw) :
{       
  "registration_ids":["yours"],
  "data": {
    "Hello" : "World"
  } 
}

on success you will get

Response :
{
  "multicast_id": 6506103988515583000,
  "success": 1,
  "failure": 0,
  "canonical_ids": 0,
  "results": [
    {
      "message_id": "0:1432811719975865%54f79db3f9fd7ecd"
    }
  ]
}
Michael
  • 9,639
  • 3
  • 64
  • 69
18

Pushwatch is a free to use online GCM and APNS push notification tester developed by myself in Django/Python as I have found myself in a similar situation while working on multiple projects. It can send both GCM and APNS notifications and also support JSON messages for extra arguments. Following are the links to the testers.

Please let me know if you have any questions or face issues using it.

Amyth
  • 32,527
  • 26
  • 93
  • 135
  • 1
    Just what I needed, works perfectly (tried the postman one, which sorta worked, but didnt cause the message to appear on my device when my app was not opened..) – Greywire Mar 15 '16 at 19:07
  • Hi Amyth i get this error on your site: "HTTP Error 401: Invalid (legacy) Server-key delivered or Sender is not authorized to perform request." Not sure what to do? I just created a Google Project and tried using the project ID and the project Number for the SenderID and added your site to the "Accept requests from these HTTP referrers (websites)" in the google API settings. Best regards Rasmus – Thylle May 31 '18 at 13:47
  • Note this URL is no longer support: *As of April 10, 2018, Google has deprecated GCM. The GCM server and client APIs are deprecated and will be removed as soon as April 11, 2019.* [GCM info](https://developers.google.com/cloud-messaging/http-server-ref) – Stinky Towel Aug 03 '18 at 16:45
7

Postman is a good solution and so is php fiddle. However to avoid putting in the GCM URL and the header information every time, you can also use this nifty GCM Notification Test Tool

Varun
  • 1,938
  • 18
  • 19
  • 2
    You can use this online tester which supports both Android and iOS. Easy to use simple website http://www.pushtry.com 1. Select you .p12 file 2. Enter device token3 3. Select environment Sandbox or production 4. Enter message 5. Send – Arvind Aug 22 '16 at 06:11