GCM에 등록 된 ID를 새 활동에 등록하고 공유하는 프로그램을 작성했지만 불행히도 등록 버튼을 클릭하면 내 등록 ID 자체가 표시되지 않습니다. jsp 페이지에서 push push notification 버튼을 클릭하면 오류 메시지가 표시됩니다. "File Not Found Exception GCMRegId.txt"
. 누군가가 나에게 나는 다음과 같은 코드를 확인해야 변경 사항을 제안 할 수 있습니다
경우GCM 푸시 알림이 작동하지 않고 레지스터 ID도 표시되지 않음
GCMNotification.java 등록 및 다른 두 개의 버튼을 가지고
@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 = "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);
}
}
RegistrationActivity.java
에게 큰 도움이 될 것입니다 공유, On register button
을 위해 건배를 넣어 가지고하지만 난 그것을 클릭하면, 그것은 나에게 "RegId is : " , no regid is shown
를 보여주고 내가 공유 버튼을 클릭하면, 그것은 "RegId is empty"
는 GCMNotificationIntentService.java
public class GCMNotificationIntentService extends IntentService {
public static final int NOTIFICATION_ID = 1;
private NotificationManager mNotificationManager;
NotificationCompat.Builder builder;
public GCMNotificationIntentService() {
super("GcmIntentService");
}
@Override
protected void onHandleIntent(Intent intent) {
// TODO Auto-generated method stub
Bundle extras = intent.getExtras();
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
String messageType = gcm.getMessageType(intent);
if (!extras.isEmpty()) {
if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
sendNotification("Send error: " + extras.toString());
} else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) {
sendNotification("Deleted messages on server: " + extras.toString());
} else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) {
for (int i = 0; i < 3; i++) {
// Log.i(TAG, "Working... " + (i + 1) + "/5 @ " +
// SystemClock.elapsedRealtime());
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
}
}
// Log.i(TAG, "Completed work @ " +
// SystemClock.elapsedRealtime());
sendNotification("Message Received from Google GCM Server: " +
extras.get(Config.MESSAGE_KEY));
// Log.i(TAG, "Received: " + extras.toString());
}
}
GcmBroadcastReceiver.completeWakefulIntent(intent);
}
private void sendNotification(String msg) {
// Log.d(TAG, "Preparing to send notification...: " + msg);
mNotificationManager = (NotificationManager)
this.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this,
Service_Access.class), 0);
NotificationCompat.Builder mBuilder = new
NotificationCompat.Builder(this).setSmallIcon(R.drawable.gcm_logo)
.setContentTitle("GCM Notification").setStyle(new
NotificationCompat.BigTextStyle().bigText(msg))
.setContentText(msg);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
// Log.d(TAG, "Notification sent successfully.");
}
Config.java
public interface Config {
// GCM server using java
static final String APP_SERVER_URL = "http://192.168.1.17:8081/GCM-App-Server/GCMNotification?
shareRegId=1";
// Google Project Number
static final String GOOGLE_PROJECT_ID = "167543534432";
static final String MESSAGE_KEY = "message";
}
난의 index.jsp 페이지 GCM 통해 버튼 푸시 알림 클릭
, 이는을 표시 오류가 java.io.FileNotFoundException : GCMRegId.txt (시스템이 지정된 파일을 찾을 수 없음) java.io.FileInputStream.open (기본 메소드)에서 (알 수없는 소스) 에서 java.io.FileInputStream (알 수없는 소스) 에서 java.io.FileReader. (알 수없는 소스) 에서 com.android.d_governance.GCMNotification.doPost (GCMNotification. java : 55) 에서 javax.servlet.http.HttpServlet.service (HttpServlet.java:646) 에서 javax.servlet.http.HttpServlet.service (HttpServlet.java:727) at org.apache.catalina.core. ApplicationFilterChain.internalDoFilter (ApplicationFilterChain.java:303) at org.apache.catalina.core.ApplicationFilterChain.doFilter (ApplicationFilterChain.java:208) at org.apache.tomcat.websocket.server.WsFilter.doFilter (WsFilter.java : 52) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter (Application FilterChain.java:241) at org.apache.catalina.core.ApplicationFilterChain.doFilter (ApplicationFilterChain.java:208) at org.apache.catalina.core.StandardWrapperValve.invoke (StandardWrapperValve.java:220) at org. org.apache.catalina.core.StandardHostValve.invoke에서 org.apache.catalina.authenticator.AuthenticatorBase.invoke (AuthenticatorBase.java:501) 에서 apache.catalina.core.StandardContextValve.invoke (StandardContextValve.java:122) (StandardHostValve.java:170) at org.apache.catalina.valves.ErrorReportValve.invoke (ErrorReportValve.java:98) at org.apache.catalina.valves.AccessLogValve.invoke (AccessLogValve.java:950) at org. .infoke (StandardEngineValve.java:116) a org.apache.coyote.http11.AbstractHttp11Processor.process (AbstractHttp11Processor.java:1040)의 at org.apache.coyote.AbstractProtocol $ AbstractConnectionHandler .process (AbstractProtocol.java:607) at org.apache.tomcat.util.net.AprEndpoint $ SocketProcessor.doRun (AprEndpoint.자바 : 2441) org.apache.tomcat.util.net.AprEndpoint $ SocketProcessor.run (AprEndpoint.java:2430) 에서 java.util.concurrent.ThreadPoolExecutor.runWorker (알 수없는 소스) 에있는 java.util.concurrent에서 .ThreadPoolExecutor $ Worker.run (알 수없는 소스) java.lang.Thread.run에서 (알 수없는 소스)
내 로그 캣 :
01-12 12 : 05 : 07.765 : D/RegisterActivity (20445) : GCM - GCM 서버에 성공적으로 등록 - regId : 01-12 12 : 05 : 07.765 : D/RegisterActivity (20445) : GCM RegId :
GCM RegId가 없습니다.
을 보여주는 것은 당신에게
예외 스택 추적을 게시 할 수 있습니까? – shyam
@shyam 나는 내 질문에 logcat을 표시하고 오류가 나타납니다. –
아는 한, 안드로이드 앱에서 등록 된 GCM ID를 애플리케이션 서버로 보내지 않았습니다. – shyam