我正在Google Cloud Messaging(GCM)创建一个项目,并关注this tutorial。
我完成了客户端工作并在客户端设置了设备。我还使用以下代码注册了设备。
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
GCMRegistrar.checkDevice(this);
GCMRegistrar.checkManifest(this);
final String regId = GCMRegistrar.getRegistrationId(this);
if (regId.equals("")) {
GCMRegistrar.register(this, "483910217912");
Log.d(tag, "Registered");
}
else {
Log.v(tag, "Already registered");
}
}
现在我陷入了为GCM项目创建服务器的问题。请注意,我正在创建一个项目,以便在收到新消息时通知。但是,我没有实现接收消息的服务,但是我将在服务器设置完成后实现它。
答案 0 :(得分:6)
您可以使用博客文章 Google cloud Messaging (GCM) tutorial 在Android中创建GCM服务器,但我更倾向于使用PHP作为服务器端代码。您可以通过简单的步骤在cURL(PHP)中创建GCM服务器:
从Google API控制台页面创建服务器密钥。
标识要将此消息发送到的设备的设备令牌。
您可以在 How to implement a GCM PHP push server for Android 中找到简单的步骤来实现推送服务器。
答案 1 :(得分:5)
您可以使用此代码
package yourpackage.android.gcm.server;
import com.google.android.gcm.server.Message;
import com.google.android.gcm.server.MulticastResult;
import com.google.android.gcm.server.Sender;
import java.util.ArrayList;
class Notify {
public static void main(String args[]) {
try {
Sender sender = new Sender("AIzaSyCn3N2OIm-EDtiGwTyQfSIB8NRvDtIOx30");
ArrayList<String> devicesList = new ArrayList<String>();
//add you deviceID
devicesList.add("APA91bELVJbxB_NLnLbTkkkX87SDdkJc6OfCN2slhC9t4cLq-KA32eGgiW4-Gi--ZEsEMKIh0AtYJMs5rQGswfm3cH1qK853WcpV98bkaplAaC5AiycDmifuVFSRl21vgf-Rqj0dCrFF");
//devicesList.add("APA91bHIdM4XGqrjJLTuwCX5OOrTYG4ACXYEVkZDM1bPs5qFdzJP4Bpql-sZqyKB8BU7fDtdxB84aTygHLyASYg_XNY6lqrcA4wj4sZHJXGVFzz_0UEADMfFCx9NAfRZxunIYso_dkBa");
//APA91bFA-i2l3iEMnIBs0JK80pTLHOsE7p1s-DysRpKGas1MQOVILyIs9xwY7soysSWGz5Uif68uXR6F5Xn0tCTYesv78uQZxhC310a1cvf8aFohhfMGY6awbOSg3t1GRz2i3U-8kVSF
// Use this line to send message without payload data
// Message message = new Message.Builder().build();
// use this line to send message with payload data
Message message = new Message.Builder()
//.collapseKey("message")
//.timeToLive(241000)
.delayWhileIdle(true)
.addData("message", "Your message send")
.build();
/**/
// Use this code to send to a single device
// Result result = sender
// .send(message,
// "APA91bGiRaramjyohc2lKjAgFGpzBwtEmI8tJC30O89C2b3IjP1CuMeU1h9LMjKhmWuZwcXZjy1eqC4cE0tWBNt61Kx_SuMF6awzIt8WNq_4AfwflaVPHQ0wYHG_UX3snjp_U-5kJkmysdRlN6T8xChB1n3DtIq98w",
// 1);
// Use this for multicast messages
MulticastResult result = sender.send(message, devicesList, 1);
//sender.send(message, devicesList, 0);
System.out.println(result.toString());
if (result.getResults() != null) {
int canonicalRegId = result.getCanonicalIds();
if (canonicalRegId != 0) {
}
} else {
int error = result.getFailure();
System.out.println(error);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
答案 2 :(得分:5)
不推荐使用com.google.android.gcm.server
库。只需将您的消息编码为JSON对象并将其发布到GCM URL https://android.googleapis.com/gcm/send
JSON示例:
{
"registration_ids" : ["APA91bHun4MxP5egoKMwt2KZFBaFUH-1RYqx...",...],
"data" : {
"Team" : "Portugal",
"Score" : "3",
"Player" : "Varela",
},
}
答案 3 :(得分:4)
您可以在Android SDK目录中找到gcm-client和gcm-server的示例代码。入门是个好点。目录是:
path_to_android_sdk /演员/谷歌/ GCM /样品
答案 4 :(得分:3)
In your main function implement following code to send push notification to your app
final String apiKey = "specify your api key generated by gcm";
To make http connection to gcm using following code
URL url = new URL("https://android.googleapis.com/g...");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("Authorization", "key="+apiKey);
conn.setDoOutput(true);
JSON message format accepted by GCM
String input = "{\"registration_ids\" : [\"Specify token you got from GCM\"],\"data\" : {\"message\": \"hai welcome\"},}";
To send notification
OutputStream os = conn.getOutputStream();
os.write(input.getBytes());
os.flush();
In your client app you need to have proper BroadcastReceiver class to receive the message sent from GCM
答案 5 :(得分:2)