我正在尝试在我的android手机中检索gcm通知。一切运行正常,我的Android设备从gcm服务器获取registrationid我将它发送到我的PHP服务器保存它。然后我运行我的PHP脚本与该registrationID发送我的Android手机上的通知。通知没有显示,我的设备显示运行时异常(当我在我的模拟器上运行时,这个异常不会弹出)。我的IntentService类中有一些错误,我无法从我的logcat中了解。
这是我的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=muazzamalii@hotmail.com"); //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 :(得分:0)
这是您推送到设备的JSON有效负载吗?
{ "data":
{
"score": "5x1",
"time": "15:10"
},
"registration_ids": [xxxxxx]
}
上述有效负载是客户端代码工作所必需的。
在这些调用之后检查Null指针:
String score = intent.getStringExtra("score");
String time = intent.getStringExtra("time");
很可能得分和时间均为空,导致Log.i崩溃