Push notification Android后端的谷歌云消息
我制作了一个接收推送通知的应用程序。当我使用以下java代码向计算机上的发送请求时,会发送和接收通知:Push notification Android后端的谷歌云消息,push-notification,google-cloud-messaging,google-cloud-endpoints,google-cloud-sql,Push Notification,Google Cloud Messaging,Google Cloud Endpoints,Google Cloud Sql,我制作了一个接收推送通知的应用程序。当我使用以下java代码向计算机上的发送请求时,会发送和接收通知: public static void main(String[] args) throws IOException { String value = "Kent Arne Bjerke har blitt med på din event"; String url = "https://android.googleapis.com/gcm/send"; URL obj
public static void main(String[] args) throws IOException {
String value = "Kent Arne Bjerke har blitt med på din event";
String url = "https://android.googleapis.com/gcm/send";
URL obj = new URL(url);
HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
//add reuqest header
con.setRequestMethod("POST");
con.setRequestProperty("Authorization", "key=xxxxxxxxxxxxxxx");
con.setRequestProperty("Sender", "id=xxxxxxxxxx");
String urlParameters = "collapse_key=score_update&time_to_live=108&delay_while_idle=1&data.message=" + value + "®istration_id=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
// Send post request
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();
int responseCode = con.getResponseCode();
System.out.println("\nSending 'POST' request to URL : " + url);
System.out.println("Post parameters : " + urlParameters);
System.out.println("Response Code : " + responseCode);
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
//print result
System.out.println(response.toString());
}
在我的计算机上本地测试了这段代码之后,我想在我的google app engine服务器上实现这段代码。我复制了代码,并从google app engine端点方法调用了它。然后我得到了这个错误:
{
"error": {
"errors": [
{
"domain": "global",
"reason": "backendError",
"message": "java.lang.ClassCastException: com.google.apphosting.utils.security.urlfetch.URLFetchServiceStreamHandler$Conne ction cannot be cast to javax.net.ssl.HttpsURLConnection"
}
],
"code": 503,
"message": "java.lang.ClassCastException: com.google.apphosting.utils.security.urlfetch.URLFetchServiceStreamHandler$Connection cannot be cast to javax.net.ssl.HttpsURLConnection"
}
}
如果我理解正确,则不允许使用此方法调用
我正在寻找一种很好的方法来调用Android的谷歌云消息,并从我的服务器而不是客户端发送推送通知。我必须向多个接收者发送推送通知,因此它必须是异步的。我通过使用以下代码从后端服务器发送推送通知找到了解决方案:Sender Sender=new SenderAPI_KEY;Message msg=new Message.Builder.addDatamessage,Message.build;结果=sender.sendmsg,sender_id,5;请你可以给它作为一个答案,如果它为你工作,并标记为回答。