WakefulBroadcastReceiver onReceive not triggering android GCM

为君一笑 提交于 2019-12-20 06:06:51

问题


I implemented GCM notifications in Android and everything was working fine. I'm sending Notifications to the phone via a PHP server using the Google Cloud Messaging Service.
After a week or so I installed that app again on my phone and send a test notification onto my phone but I got nothing. The PHP script runs without any error with success=1 and no cononical id's in the GCM response. WakufulBroadcastReceiver isn't triggered in the android app.

Here is the complete code that I am using:

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>

In MainActivity.java I am using this task to register

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"
    }]
}

Help me! Thanks.

Note: GooglePlayServices are installed in the phone as well. I get no error while registration.


回答1:


Make sure you have

  1. Signed the build with the correct key (dev vs production)
  2. Updated your php script to send the message to the new GCM registration id


来源:https://stackoverflow.com/questions/28251934/wakefulbroadcastreceiver-onreceive-not-triggering-android-gcm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!