WakefulBroadcastReceiver onReceive不会触发android GCM

时间:2015-01-31 13:06:44

标签: android google-cloud-messaging android-broadcast

我在Android中实施了GCM次通知,一切正常。我正在使用Google云消息传递服务通过PHP服务器向手机发送通知 大概一周后,我再次在手机上安装了该应用程序并将测试通知发送到我的手机上,但我什么也没得到。 PHP脚本在success=1运行时没有任何错误,GCM响应中没有cononical id。在Android应用中没有触发WakufulBroadcastReceiver

以下是我正在使用的完整代码:

GcmBroadcastReceiver.java

public class GcmBroadcastReceiver extends WakefulBroadcastReceiver{
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d("", "In Receive Method of Broadcast Receiver");
        ComponentName cn = new ComponentName(context.getPackageName(), GcmMessageHandler.class.getName());
        startWakefulService(context, intent.setComponent(cn));
        setResultCode(Activity.RESULT_OK);
    }
}

GcmMessageHandler.java

public class GcmMessageHandler extends IntentService{

    Handler handler;
    public GcmMessageHandler() {
        super("GcmMessageHandler");
        // TODO Auto-generated constructor stub
    }
    public GcmMessageHandler(String name) {
        super("GcmMessageHandler");
    }

    @Override
    public void onCreate() {
        super.onCreate();
        handler = new Handler();
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
        String messageType = gcm.getMessageType(intent);
        final String message = intent.getExtras().getString("name");
         Log.d("GCM", "Received : (" +messageType+")  "+intent.getExtras().getString("name"));
        handler.post(new Runnable() {

            @Override
            public void run() {
                Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();
            }
        });
        GcmBroadcastReceiver.completeWakefulIntent(intent);

    }

}

的Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.test_push_app"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    <permission
        android:name="com.example.test_push_app.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />
    <uses-permission android:name="com.example.test_push_app.permission.C2D_MESSAGE" />


    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <receiver
            android:name=".GcmBroadcastReceiver"
            android:permission="com.google.android.c2dm.permission.SEND" >
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <category android:name="com.example.test_push_app" />
            </intent-filter>
        </receiver>
        <service android:name=".GcmMessageHandler" />

        <meta-data android:name="com.google.android.gms.version"
           android:value="@integer/google_play_services_version" />
    </application>

</manifest>

MainActivity.java 中,我正在使用此任务注册

private class Register extends AsyncTask<Void, Void, String>{

        String regID;

        @Override
        protected String doInBackground(Void... params) {
            try{
                regID = gcm.register("40441*******");

            }catch(Exception ex){
                regID = "Failed";
                //Log.d("", ex.getMessage());
                ex.printStackTrace();
            }
            return regID;
        }
        @Override
        protected void onPostExecute(String result) {
            etRegid.setText(result);
            Log.d("", result);
            super.onPostExecute(result);
        }

    }

PhpResponse:

{
    "multicast_id": 7260653358308753695,
    "success": 1,
    "failure": 0,
    "canonical_ids": 0,
    "results": [{
        "message_id": "0:1422710889970517%ef663a58f9fd7ecd"
    }]
}

救救我!感谢。

注意:GooglePlayServices也安装在手机中。注册时我没有错误。

1 个答案:

答案 0 :(得分:0)

确保你有

  1. 使用正确的密钥(dev vs production)签署构建
  2. 更新了您的php脚本,将消息发送到新的GCM注册ID