Android: 在线测试推送通知(Google云消息传递)

更新: 不推荐使用 GCM,请使用 < a href = “ https://firebase.google.com/docs/cloud-message/”rel = “ nofollow noReferrer”> FCM

我正在申请中实施 Google云消息传递。服务器代码还没有准备好,在我的环境中,由于一些防火墙限制,我不能部署一个测试服务器用于推送通知。我要寻找的是一个在线服务器,将发送一些测试通知到我的设备,以测试我的客户端实现。

117661 次浏览

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

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"
}
]
}

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

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.