2012-07-04 3 views
1

안드로이드 모바일에서 gcm 알림을 검색하려고합니다. 모든 것이 잘 실행되고, 내 안드로이드 장치는 gcm 서버에서 registrationid를 가져옵니다. 내 안드로이드 mobile.The 알림 표시되지 않습니다 및 내 장치 런타임 예외 (이 예외가 내 에뮬레이터에서 실행할 때 팝업되지 않습니다) 표시 알림을 보내려면 해당 registrationID 내 PHP 스크립트를 실행합니다. 뭔가 잘못입니다. 내 IntentService 클래스에서 나는 내 logcat에서 이해할 수 없다.모바일에서 gcm 알림을 수신 할 수 없음 : Android

다음
package com.pack.gcm; 

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 

import org.apache.http.HttpEntity; 
import org.apache.http.HttpResponse; 
import org.apache.http.client.ClientProtocolException; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.methods.HttpGet; 
import org.apache.http.impl.client.DefaultHttpClient; 

import android.app.IntentService; 
import android.content.Context; 
import android.content.Intent; 
import android.os.PowerManager; 
import android.util.Log; 

import android.widget.Toast; 

public class MyIntentService extends IntentService { 

public MyIntentService() { 
    super("MuazzamService"); 
} 

private static PowerManager.WakeLock sWakeLock; 
    private static final Object LOCK = MyIntentService.class; 



@Override 
protected void onHandleIntent(Intent intent) { 

    try { 
      String action = intent.getAction(); 
      if (action.equals("com.google.android.c2dm.intent.REGISTRATION")) { 
       handleRegistration(intent); 
      } else if (action.equals("com.google.android.c2dm.intent.RECEIVE")) { 
       handleMessage(intent); 
      } 
     } finally { 
      synchronized(LOCK) { 
       sWakeLock.release(); 
      } 
     } 
} 

private void handleRegistration(Intent intent) { 
     try { 
     String registrationId = intent.getStringExtra("registration_id"); 
     String error = intent.getStringExtra("error"); 
     String unregistered = intent.getStringExtra("unregistered");  
     // registration succeeded 
     if (registrationId != null) { 

      this.SendRegistrationIDViaHttp(registrationId); 
      Log.i("Regid",registrationId); 
     } 

     if (unregistered != null) { 
     } 

     if (error != null) { 
      if ("SERVICE_NOT_AVAILABLE".equals(error)) { 
       Log.e("ServiceNoAvail",error); 

      } 
      else { 
       Log.i("Error In Recieveing regid", "Received error: " + error); 
      } 
     } 
     }catch(Exception e) 
     { 
      Log.e("ErrorHai(MIS0)",e.toString()); 
      e.printStackTrace(); 
     } 
} 

private void SendRegistrationIDViaHttp(String regID) { 

    HttpClient httpclient = new DefaultHttpClient(); 
try 
{ 
    Context context = getApplicationContext(); 

    HttpGet httpget = new 
      HttpGet("http://192.168.1.12/php/GCM/AndroidRequest.php?registrationID="+regID+"&[email protected]"); //test purposes k liye muazzam 
    HttpResponse response = httpclient.execute(httpget); 
    HttpEntity entity=response.getEntity(); 
    if(entity!=null) 
    { 
      InputStream inputStream=entity.getContent(); 
      String result= convertStreamToString(inputStream); 
      Log.i("finalAnswer",result); 
      Toast tst=Toast.makeText(context,regID, Toast.LENGTH_SHORT); 
      tst.show(); 
    } 
} 
catch (ClientProtocolException e) 
{ 
    Log.e("errorhai",e.getMessage()); 
    e.printStackTrace(); 
} 
catch (IOException e) 
{ 
    Log.e("errorhai",e.getMessage()); 
    e.printStackTrace(); 
} 
} 
    private static String convertStreamToString(InputStream is) { 

    BufferedReader reader = new BufferedReader(new InputStreamReader(is)); 
    StringBuilder sb = new StringBuilder(); 

    String line = null; 
    try { 
     while ((line = reader.readLine()) != null) { 
      sb.append(line + "\n"); 
     } 
    } catch (IOException e) { 
     Log.e("ErrorHai(MIS)",e.toString()); 
     e.printStackTrace(); 
    } finally { 
     try { 
      is.close(); 
     } catch (IOException e) { 
      Log.e("ErrorHai(MIS2)",e.toString()); 
      e.printStackTrace(); 
     } 
    } 
    return sb.toString(); 
} 

private void handleMessage(Intent intent) { 
    String score = intent.getStringExtra("score"); 
    String time = intent.getStringExtra("time"); 

    Log.i("GetExtraScore",score); 
    Log.i("GetExtratime",time); 
} 

static void runIntentInService(Context context,Intent intent){ 
    synchronized(LOCK) { 

     if (sWakeLock == null) { 
       PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE); 
       sWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "my_wakelock"); 
      } 
     } 
     sWakeLock.acquire(); 
     intent.setClassName(context, MyIntentService.class.getName()); 
     context.startService(intent); 
} 

} 

내 로그 캣입니다 :

여기 내 IntentService 클래스의

07-04 23:00:41.979: E/AndroidRuntime(9987): FATAL EXCEPTION: IntentService[MuazzamService] 
07-04 23:00:41.979: E/AndroidRuntime(9987): java.lang.NullPointerException: println needs a message 
07-04 23:00:41.979: E/AndroidRuntime(9987): at android.util.Log.println_native(Native Method) 
07-04 23:00:41.979: E/AndroidRuntime(9987): at android.util.Log.i(Log.java:158) 
07-04 23:00:41.979: E/AndroidRuntime(9987): at com.pack.gcm.MyIntentService.handleMessage(MyIntentService.java:139) 
07-04 23:00:41.979: E/AndroidRuntime(9987): at com.pack.gcm.MyIntentService.onHandleIntent(MyIntentService.java:41) 
07-04 23:00:41.979: E/AndroidRuntime(9987): at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:59) 
07-04 23:00:41.979: E/AndroidRuntime(9987): at android.os.Handler.dispatchMessage(Handler.java:99) 
07-04 23:00:41.979: E/AndroidRuntime(9987): at android.os.Looper.loop(Looper.java:123) 
07-04 23:00:41.979: E/AndroidRuntime(9987): at android.os.HandlerThread.run(HandlerThread.java:60) 
07-04 23:01:48.089: E/AndroidRuntime(10099): FATAL EXCEPTION: IntentService[MuazzamService] 
07-04 23:01:48.089: E/AndroidRuntime(10099): java.lang.NullPointerException: println needs a message 
07-04 23:01:48.089: E/AndroidRuntime(10099): at android.util.Log.println_native(Native Method) 
07-04 23:01:48.089: E/AndroidRuntime(10099): at android.util.Log.i(Log.java:158) 
07-04 23:01:48.089: E/AndroidRuntime(10099): at com.pack.gcm.MyIntentService.handleMessage(MyIntentService.java:139)  
07-04 23:01:48.089: E/AndroidRuntime(10099): at com.pack.gcm.MyIntentService.onHandleIntent(MyIntentService.java:41) 
07-04 23:01:48.089: E/AndroidRuntime(10099): at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:59) 
07-04 23:01:48.089: E/AndroidRuntime(10099): at android.os.Handler.dispatchMessage(Handler.java:99) 
07-04 23:01:48.089: E/AndroidRuntime(10099): at android.os.Looper.loop(Looper.java:123) 
07-04 23:01:48.089: E/AndroidRuntime(10099): at android.os.HandlerThread.run(HandlerThread.java:60) 
07-04 23:11:36.189: E/AndroidRuntime(10310): FATAL EXCEPTION: IntentService[MuazzamService] 
07-04 23:11:36.189: E/AndroidRuntime(10310): java.lang.NullPointerException: println needs a message 
07-04 23:11:36.189: E/AndroidRuntime(10310): at android.util.Log.println_native(Native Method) 
07-04 23:11:36.189: E/AndroidRuntime(10310): at android.util.Log.i(Log.java:158) 
07-04 23:11:36.189: E/AndroidRuntime(10310): at com.pack.gcm.MyIntentService.handleMessage(MyIntentService.java:139) 
07-04 23:11:36.189: E/AndroidRuntime(10310): at com.pack.gcm.MyIntentService.onHandleIntent(MyIntentService.java:41) 
07-04 23:11:36.189: E/AndroidRuntime(10310): at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:59) 
07-04 23:11:36.189: E/AndroidRuntime(10310): at android.os.Handler.dispatchMessage(Handler.java:99) 
07-04 23:11:36.189: E/AndroidRuntime(10310): at android.os.Looper.loop(Looper.java:123) 
07-04 23:11:36.189: E/AndroidRuntime(10310): at android.os.HandlerThread.run(HandlerThread.java:60) 
07-04 23:12:31.739: E/AndroidRuntime(10482): FATAL EXCEPTION: IntentService[MuazzamService] 
07-04 23:12:31.739: E/AndroidRuntime(10482): java.lang.NullPointerException: println needs a message 
07-04 23:12:31.739: E/AndroidRuntime(10482): at android.util.Log.println_native(Native Method) 
07-04 23:12:31.739: E/AndroidRuntime(10482): at android.util.Log.i(Log.java:158) 
07-04 23:12:31.739: E/AndroidRuntime(10482): at com.pack.gcm.MyIntentService.handleMessage(MyIntentService.java:139) 
07-04 23:12:31.739: E/AndroidRuntime(10482): at com.pack.gcm.MyIntentService.onHandleIntent(MyIntentService.java:41) 
07-04 23:12:31.739: E/AndroidRuntime(10482): at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:59) 
07-04 23:12:31.739: E/AndroidRuntime(10482): at android.os.Handler.dispatchMessage(Handler.java:99) 
07-04 23:12:31.739: E/AndroidRuntime(10482): at android.os.Looper.loop(Looper.java:123) 
07-04 23:12:31.739: E/AndroidRuntime(10482): at android.os.HandlerThread.run(HandlerThread.java:60) 
07-04 23:13:57.199: E/AndroidRuntime(10541): FATAL EXCEPTION: IntentService[MuazzamService] 
07-04 23:13:57.199: E/AndroidRuntime(10541): java.lang.NullPointerException: println needs a message 
07-04 23:13:57.199: E/AndroidRuntime(10541): at android.util.Log.println_native(Native Method) 
07-04 23:13:57.199: E/AndroidRuntime(10541): at android.util.Log.i(Log.java:158) 
07-04 23:13:57.199: E/AndroidRuntime(10541): at com.pack.gcm.MyIntentService.handleMessage(MyIntentService.java:139) 
07-04 23:13:57.199: E/AndroidRuntime(10541): at com.pack.gcm.MyIntentService.onHandleIntent(MyIntentService.java:41) 
07-04 23:13:57.199: E/AndroidRuntime(10541): at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:59) 
07-04 23:13:57.199: E/AndroidRuntime(10541): at android.os.Handler.dispatchMessage(Handler.java:99) 
07-04 23:13:57.199: E/AndroidRuntime(10541): at android.os.Looper.loop(Looper.java:123) 
07-04 23:13:57.199: E/AndroidRuntime(10541): at android.os.HandlerThread.run(HandlerThread.java:60) 

업데이트 :

$message = '{"data":{"message":"one","some":"free"},"registration_ids":["xxxx"]}'; 
:

이 내가 내 PHP 스크립트에서 보내는 것입니다

그리고 이것이 제가 얻은 결과입니다.

{"multicast_id":8230995787169744326,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1341431581592147%5d17b789f9fd7ecd"}]}8.2309957871697E+18 

답변

0

기기로 푸시하는 JSON 페이로드입니까?

{ "data": 
    { 
    "score": "5x1", 
    "time": "15:10" 
    }, 
    "registration_ids": [xxxxxx] 
} 

위의 페이로드는 클라이언트 코드가 작동하는 데 필요합니다.

이러한 호출 후 널 포인터 체크를 넣어 :

String score = intent.getStringExtra("score"); 
String time = intent.getStringExtra("time"); 

대부분의 경우 점수와 시간이 널 (null), 그리고 원인이 모두있는 Log.i가

+0

그래, 당신이 바로 그것을주고있다'nullpointerexception'을 충돌 nullpointerexception을 잡았고'Log.e'에서 프린트하려하지만 로그에 아무 것도 출력하지 않습니다. – Mj1992

+0

오 thnx 나는 거기에서 메시지를 보내고 점수와 시간을 reciveing ​​여기 내 나쁜했다. – Mj1992

+0

@ Mj1992 당신은 문제를 해결하기 위해 무엇을했는지 설명해 주실 수 있습니까? becoz 또한 내 전화에 gcm 알림을받지 못했습니다. 내 서버에 상태 200 인 gcm 회신 – user1170793

관련 문제