为Google Cloud Messaging设置服务器端

时间:2013-07-10 06:29:25

标签: android server-side google-cloud-messaging

我最近一直在学习Android开发,我正在尝试制作一个使用Google Cloud Messaging的示例应用程序。我的目标是创建一个可以从服务器接收推送通知的简单应用程序。我通过注册我的设备让应用程序的客户端工作。现在我正在尝试创建服务器端。但是,我完全没有在服务器端设置服务器或编程的经验。所以我希望有人可以指出我正确的方向,以便我可以让服务器发送推送通知。我一直在关注this link的教程,但我坚持服务器实现。如果有人能指出我正确的方向,我将不胜感激。谢谢!

2 个答案:

答案 0 :(得分:6)

使用Tomcat或AppEngine实际上更容易。 See this tutorial in how to setup your GCM Server.

您需要在服务器端向其发送消息的设备注册ID,您需要API密钥,这是一个JSP示例:

http://yourdomain.com:8080/sendMessage.jsp?registrationID=kSADAS3242&messageToSend=Hello

String value = request.request.getParameter("messageToSend");
String registrationId = request.getParameter("registrationID");
Sender sender = new Sender("YOUR API KEY");
Message message = new Message.Builder().addData("FLAG","SERVE").addData("MSG", value).build();
Result result = sender.send(message, registrationId, 5);

在您的客户端设备上应该:

@Override
protected void onMessage(Context context, Intent intent) {
    Log.i(TAG, "Got a message from Google Cloud Messaging !!");
    String tag = intent.getExtras().getString("FLAG");
    String message = intent.getExtras().getString("MSG");
    Log.i(TAG, tag + " : " + message);
}

这应该打印“SERVE:Hello”

答案 1 :(得分:1)

如果你使用过PHP,你应该熟悉xampp或类似的软件。

如果没有,您只需下载并安装它,启动服务并在浏览器上转到:

http://localhost/xampp 

测试它是否正确安装。

如果您可以看到Xampp页面,您可以从xampp / htdocs开始运行php脚本并运行它们:

http://localhost/yourscript.php

尝试一个简单的问候世界:

<?php 

echo 'hello world';

?> 

之后,您应该准备好开始关注this tutorial或Google中的任何教程,只需输入gcm php tutorial

我发现php是我为GCM配置服务器端最简单的方法,希望你觉得它很有用......

相关问题