Android:在线测试推送通知(Google Cloud Messaging) [英] Android: Test Push Notification online (Google Cloud Messaging)

查看:35
本文介绍了Android:在线测试推送通知(Google Cloud Messaging)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新: GCM 已弃用,请使用 FCM

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

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.

推荐答案

找到了一个非常简单的方法.

Found a very easy way to do this.

打开http://phpfiddle.org/

在框中粘贴以下 php 脚本.在php脚本设置API_ACCESS_KEY中,设置coma分隔的设备id.

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

按 F9 或单击运行.

Press F9 or click Run.

玩得开心;)

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

对于 FCM,google url 将是:https://fcm.googleapis.com/fcm/send

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

对于 FCM v1 google url 将是:https://fcm.googleapis.com/v1/projects/YOUR_GOOGLE_CONSOLE_PROJECT_ID/messages:send

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

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

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

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

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

一些响应代码:

以下是您可能从服务器收到的一些响应代码的说明.

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

这篇关于Android:在线测试推送通知(Google Cloud Messaging)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆