我需要为 gcm 的服务器端实现一个独立的应用程序,以将通知推送到设备。除了“入门”页面上的参考之外,我还能获得任何参考。人们对 xmpp 说了些什么。我们需要使用这个还是我们可以直接使用gcm服务器端方法。帮助。或者有没有其他简单的方法来实现这个。我希望我能正确地提出我的问题。
问问题
8216 次
3 回答
3
这是Java的GCM服务器端实现的好教程。网址:java gcm 服务器端实现
示例代码:java gcm 服务器端实现`{
new Thread(){
public void run(){
try {
//Please add here your project API key: "Key for browser apps (with referers)".
//If you added "API key Key for server apps (with IP locking)" or "Key for Android apps (with certificates)" here
//then you may get error responses.
Sender sender = new Sender("AIzaSyB7Ej255tpTaemk_-Ljmn4GcklldT14Hp4");
// use this to send message with payload data
Message message = new Message.Builder()
.collapseKey("message")
.timeToLive(3)
.delayWhileIdle(true)
.addData("message", "Welcome to Push Notifications") //you can get this message on client side app
.build();
//Use this code to send notification message to a single device
Result result = sender.send(message,
"APA91bEbKqwTbvvRuc24vAYljcrhslOw-jXBqozgH8C2OB3H8R7U00NbIf1xp151ptweX9VkZXyHMik022cNrEETm7eM0Z2JnFksWEw1niJ2sQfU3BjQGiGMq8KsaQ7E0jpz8YKJNbzkTYotLfmertE3K7RsJ1_hAA",
1);
System.out.println("Message Result: "+result.toString()); //Print message result on console
//Use this code to send notification message to multiple devices
ArrayList<String> devicesList = new ArrayList<String>();
//add your devices RegisterationID, one for each device
devicesList.add("APA91bEbKqwTbvvRuc24vAYljcrhslOw-jXBqozgH8C2OB3H8R7U00NbIf1xp151ptweX9VkZXyHMik022cNrEETm7eM0Z2JnFksWEw1niJ2sQfU3BjQGiGMq8KsaQ7E0jpz8YKJNbzkTYotLfmertE3K7RsJ1_hAA");
devicesList.add("APA91bEVcqKmPnESzgnGpEstHHymcpOwv52THv6u6u2Rl-PaMI4mU3Wkb9bZtuHp4NLs4snBl7aXXVkNn-IPEInGO2jEBnBI_oKEdrEoTo9BpY0i6a0QHeq8LDZd_XRzGRSv_R0rjzzZ1b6jXY60QqAI4P3PL79hMg");
//Use this code for multicast messages
MulticastResult multicastResult = sender.send(message, devicesList, 0);
System.out.println("Message Result: "+multicastResult.toString());//Print multicast message result on console
} catch (Exception e) {
e.printStackTrace();
}
}
}.start();
}`
于 2015-09-02T19:23:29.793 回答
1
为 Java 实现 GCM 服务器端的最简单方法是使用 restful POST。网址:“ https://android.googleapis.com/gcm/send ”
示例代码:使用 scribe 框架作为使用者
public void pushToAndroidDevice(String deviceToken, String data) {
OAuthRequest request = new OAuthRequest(Verb.POST, "https://android.googleapis.com/gcm/send");
request.addHeader("Authorization", "key=" + apiKey);
request.addHeader("Content-Type", "application/json");
request.addPayload(data);
Response response = request.send();
}
于 2014-02-19T04:28:21.283 回答
0
有 2 种方法可以为 GCM 连接实现服务器
1) XMPP
2) HTTP
不同之处在于 XMPP 允许您从设备向服务器(双向)获取响应,而对于 GCM,HTTP 是(单向),您只能向设备发送推送通知。
如果您需要 Java 客户端和 HTTP 服务器的完整实现,这里是 GCM 客户端和服务器的链接
于 2014-04-06T20:46:32.703 回答