Android:在线测试推送通知(Google Cloud Messaging)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22168819/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:36:31  来源:igfitidea点击:

Android: Test Push Notification online (Google Cloud Messaging)

androidpush-notificationgoogle-cloud-messagingandroid-notificationsserver-push

提问by Adnan

Update:GCMis deprecated, use FCM

更新:不推荐使用GCM,使用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.

我正在我的应用程序中实施 Google Cloud Messaging。服务器代码尚未准备好,在我的环境中,由于某些防火墙限制,我无法部署测试服务器以进行推送通知。我正在寻找的是一个在线服务器,它会向我的设备发送一些测试通知以测试我的客户端实现。

回答by Adnan

Found a very easy way to do this.

找到了一个非常简单的方法来做到这一点。

Open http://phpfiddle.org/

打开http://phpfiddle.org/

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

将以下 php 脚本粘贴到框中。在 php 脚本设置 API_ACCESS_KEY 中,设置以 coma 分隔的设备 ID。

Press F9 or click Run.

按 F9 或单击运行。

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;
?>

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

注意:在 google 开发者控制台上创建 API Access Key 时,您必须使用 0.0.0.0/0 作为 IP 地址。(用于测试目的)。

Edit:

编辑:

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:

如果收到来自 GCM 服务器的无效注册响应,请交叉检查您的设备令牌的有效性。您可以使用以下网址检查设备令牌的有效性:

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

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

回答by Adnan

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

回答by Amyth

Pushwatchis 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 GCMand APNSnotifications and also support JSON messages for extra arguments. Following are the links to the testers.

Pushwatch是一个免费使用的在线 GCM 和 APNS 推送通知测试器,由我自己在 Django/Python 中开发,因为我在处理多个项目时发现自己处于类似的情况。它可以发送GCMAPNS通知,还支持额外参数的 JSON 消息。以下是测试人员的链接。

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

如果您有任何疑问或在使用它时遇到问题,请告诉我。

回答by Varun

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

Postman 是一个很好的解决方案,php fiddle 也是一个很好的解决方案。但是为了避免每次都输入 GCM URL 和标题信息,您也可以使用这个漂亮的GCM 通知测试工具