我编写了一个程序,使用jsp在Android中实现Google Cloud Messaging。从这里得到了参考(http://javapapers.com/android/google-cloud-messaging-gcm-for-android-and-push-notifications/)。 但是当我点击index.jsp页面上的“通过GCM发送推送通知”页面
时,我收到错误“”需要RegId:java.io.FileNotFoundException:GCMRegId.txt(系统找不到指定的文件)“”
这是定义了GCMRegId.txt的文件,但服务器找不到该文件,因此如果有人实施了GCM,可以建议我需要做哪些更改。
@WebServlet("/GCMNotification")
public class GCMNotification extends HttpServlet {
private static final long serialVersionUID = 1L;
// Put your Google API Server Key here
private static final String GOOGLE_SERVER_KEY = "My Server Key";
static final String MESSAGE_KEY = "message";
public GCMNotification() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)throws
ServletException, IOException {
doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException {
Result result = null;
String share = request.getParameter("shareRegId");
// GCM RedgId of Android device to send push notification
String regId = "";
if (share != null && !share.isEmpty()) {
regId = request.getParameter("regId");
PrintWriter writer = new PrintWriter("GCMRegId.txt");
writer.println(regId);
writer.close();
request.setAttribute("pushStatus", "GCM RegId Received.");
request.getRequestDispatcher("index.jsp").forward(request, response);
} else {
try {
BufferedReader br = new BufferedReader(new FileReader("GCMRegId.txt"));
regId = br.readLine();
br.close();
String userMessage = request.getParameter("message");
Sender sender = new Sender(GOOGLE_SERVER_KEY);
Message message = new Message.Builder().timeToLive(30).delayWhileIdle(true)
.addData(MESSAGE_KEY, userMessage).build();
System.out.println("regId: " + regId);
result = sender.send(message, regId, 1);
request.setAttribute("pushStatus", result.toString());
} catch (IOException ioe) {
ioe.printStackTrace();
request.setAttribute("pushStatus", "RegId required: " + ioe.toString());
} catch (Exception e) {
e.printStackTrace();
request.setAttribute("pushStatus", e.toString());
}
request.getRequestDispatcher("index.jsp").forward(request, response);
}
}
}
解决错误需要一些帮助 感谢你