Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/224.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法在移动设备上接收gcm通知:Android_Android_Google Cloud Messaging - Fatal编程技术网

无法在移动设备上接收gcm通知:Android

无法在移动设备上接收gcm通知:Android,android,google-cloud-messaging,Android,Google Cloud Messaging,我正在尝试在我的android手机中检索gcm通知。一切正常,我的android设备从gcm服务器获取注册ID,我将其发送到我的PHP服务器保存。然后我使用该注册ID运行PHP脚本,在我的android手机上发送通知。通知不会显示,我的设备显示运行时异常(当我在模拟器上运行时,不会弹出此异常)。我的IntentService类中有一些错误,我无法从logcat中理解 这是我的IntentService课程: package com.pack.gcm; import java.io.Buffer

我正在尝试在我的android手机中检索gcm通知。一切正常,我的android设备从gcm服务器获取注册ID,我将其发送到我的PHP服务器保存。然后我使用该注册ID运行PHP脚本,在我的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);
}

}
这是我的日志:

 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

这是您推送到设备上的JSON负载吗

{ "data": 
  {
    "score": "5x1",
    "time": "15:10"
  },
  "registration_ids": [xxxxxx]
}
上述有效负载是客户端代码工作所必需的

在这些调用之后进行空指针检查:

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

很可能分数和时间都为空,导致日志崩溃。我崩溃了

是的,你是对的,它给出了
nullpointerexception
我捕获了nullpointerexception,并试图在
Log.e
中打印它,但它没有在日志中打印任何内容。哦,我收到了,我正在从那里发送消息,并接收分数和时间h这是我的错。@Mj1992您能解释一下您为解决您的问题做了哪些额外的工作吗?因为我的手机上也没有收到gcm通知,尽管gcm以状态200回复我的邮件server@azgolfer正确指出。问题是我没有从我的php脚本发送
score
time
。相反,我更改了变量e name to
message
我在这里收到了
score
,也就是在我的android应用程序中收到的,这就是它导致问题的原因。确保您从json发送的变量在android中以正确的变量名正确接收。