2012-07-04 43 views
1

我正在尝试在我的android移动设备中检索gcm通知。一切运行正常,我的android设备从gcm服务器获取registrationid我将其发送到我的PHP服务器保存。我使用该注册ID运行我的PHP脚本,以在我的android mobile上发送通知。通知不会显示,并且我的设备显示运行时异常(当我在模拟器上运行时,此异常不会弹出)。有问题在我的IntentService类中,我无法从logcat中理解它。无法接收移动设备上的gcm通知:Android

这里是我的IntentService类:

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); 
} 

} 

这里是我的logcat:

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) 

更新:

这是我从我的PHP脚本发送:

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

这就是我得到的结果。

{"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"); 

最有可能的分数和时间都是零,并造成Log.i崩溃

+0

是的,你是对的它给'nullpointerexception'我已经捕获了nullpointerexception并试图在'Log.e'中打印它,但它不会在日志中打印任何东西。 – Mj1992

+0

oh thnx我知道了,我从那里发送消息,并在这里收回比分和时间。 – Mj1992

+0

@ Mj1992可以请你解释你做了什么额外的解决你的问题becoz我也没有得到gcm通知我的手机虽然gcm回复状态200到我的服务器 – user1170793

相关问题