Android 的 BroadcastReceiver 未在 GCM 中调用

Android's BroadcastReceiver not called in GCM


目前我正在创建一个支持使用 GCM 在用户之间聊天的应用程序,一切正常,除了当我单击通过通知收到的消息时,聊天 activity 打开并显示空数据。

我已经在清单中注册了 GCM 广播接收器,如下所示:

<receiver
            android:name=".GcmBroadcastReceiver"
            android:permission="com.google.android.c2dm.permission.SEND" >
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <!-- Receives the registration id. -->
                <action android:name="com.google.android.c2dm.intent.REGISTRATION" />

                <category android:name="xxx.xxx.xxx" />
            </intent-filter>
        </receiver>

在聊天 activity 上,我有这样的事情:

BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {

            Bundle b = intent.getExtras();

            String message = b.getString("message");

            Log.i(TAG, " Received in Activity " + message + ", NAME = "
                    + chattingToName + ", dev ID = " + chattingToDeviceID);

            // this demo this is the same device
            ChatPeople curChatObj = addToChat(chattingToName, message,
                    "Received");
            addToDB(curChatObj); // adding to db

            populateChatMessages();

        }
    };

我猜这部分没有被调用,因为聊天记录没有用收到的消息更新。

伙计们有什么想法吗?

提前致谢。

这是我的 GcmBroadcastReceiver

public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {

        // Explicitly specify that GcmIntentService will handle the intent.
        ComponentName comp = new ComponentName(context.getPackageName(),
                GcmIntentService.class.getName());
        // Start the service, keeping the device awake while it is launching.
        startWakefulService(context, (intent.setComponent(comp)));
        setResultCode(Activity.RESULT_OK);

        Bundle extras = intent.getExtras();
        Intent i = new Intent("CHAT_MESSAGE_RECEIVED");
        i.putExtra("message", extras.getString("message"));

        context.sendBroadcast(i);

    }
}

您似乎没有以正确的方式处理从聊天应用程序收到的用户通知。请查看处理 User Notification on GCM.

的官方 Google 文档

另请查看此 tutorial,其中包含聊天应用程序的代码。

希望对您有所帮助!!