接收从Google Cloud Messaging发送的消息 - Android

时间:2016-01-30 08:09:07

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

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

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);
    }
}

IntentService

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>

1 个答案:

答案 0 :(得分:1)

好的..所以有一些事情需要在这里修复。首先是扩展GcmListenerService(实际上它继承自IntentService,但为你完成剩下的工作),然后使用intent filter声明它

my-directive

如果可能,请尽量使用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提供的服务和接收器。

`

相关问题