1

我在接收从 GCM 发送的消息时遇到了很多麻烦。我的WakefulBroadcastReceiver永远不会被解雇。我四处寻找很多帮助,但没有用。对此的任何帮助将不胜感激!

唤醒广播接收器

public class GCMBroadcastReceiver extends WakefulBroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.e("TAG", "in BroadcastReceiver");
        ComponentName componentName = new ComponentName(context.getPackageName(), MyGcmListenerService.class.getName());
        intent.setComponent(componentName);
        startWakefulService(context, intent);
        setResultCode(Activity.RESULT_OK);
    }
}

意向服务

public class MyGcmListenerService extends IntentService {

    public static final String TAG = MyGcmListenerService.class.getSimpleName();

    public MyGcmListenerService() {
        super(TAG);
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        Log.e("TAG", "in HandleIntent");
        Bundle extras = intent.getExtras();
        GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
        String messageType = gcm.getMessageType(intent);

        if (!extras.isEmpty()) {
            if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR
                    .equals(messageType)) {
                Log.e("Send error: ", extras.toString());
            } else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED
                    .equals(messageType)) {
                Log.e("Deleted messages: ",
                        extras.toString());
            } else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE
                    .equals(messageType)) {
                for (int i=0; i<5; i++) {
                    Log.i(TAG, "Working... " + (i+1)
                            + "/5 @ " + SystemClock.elapsedRealtime());
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                Log.i(TAG, "Completed work @ " + SystemClock.elapsedRealtime());
                String msg = extras.getString("message");
                String urlImg = extras.getString("image");
                String title = extras.getString("title");
                Log.e("GCM Mesage: ", "msg: " + msg + " title: " + title);
                Toast.makeText(MyGcmListenerService.this, "msg: " + msg + " title: " + title, Toast.LENGTH_LONG).show();
            }
        }
        GCMBroadcastReceiver.completeWakefulIntent(intent);
    }
}

显现

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="org.purepush.chatdemo">


    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <permission
        android:name="org.purepush.chatdemo.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />

    <uses-permission android:name="org.purepush.chatdemo.permission.C2D_MESSAGE" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">


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

        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".ChatActivity" />

        <receiver
            android:name=".service.GCMBroadcastReceiver"
            android:permission="com.google.android.c2dm.permission.SEND" >
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <action android:name="com.google.android.c2dm.intent.REGISTRATION" />

                <category android:name="org.purepush.chatdemo" />
            </intent-filter>
        </receiver>

        <service
            android:name=".service.MyGcmListenerService"/>
        <service
            android:name=".service.MyInstanceIDListenerService"
            android:exported="false">
            <intent-filter>
                <action android:name="com.google.android.gms.iid.InstanceID" />
            </intent-filter>
        </service>
        <service android:name=".service.RegistrationIntentService" />
    </application>

</manifest>
4

1 回答 1

1

好的..所以这里有几件事需要解决。首先是扩展 GcmListenerService (它实际上继承自 IntentService 但为您完成其余工作),然后使用意图过滤器声明它

<action android:name="com.google.android.c2dm.intent.RECEIVE" />

此外,如果可能,请尝试坚持使用 Google 提供的 GcmReceiver,我相信它继承自 WakefulBroadcastReceiver(https://developers.google.com/android/reference/com/google/android/gms/gcm/GcmReceiver)。

您可以在https://github.com/ucsunil/android-gcm-practice/tree/master/android-gcm-chat找到我正在处理的一个非常简单的示例- 一个聊天应用程序(非常类似于我相信你正在尝试但后来停止了它。我记得查看日志,我记得它会正确接收消息,您可以以此为例。我使用了 Gcm 提供的服务和接收器。

`

于 2016-01-30T09:05:06.143 回答