Android BroadcastReceiver未在GCM中调用

时间:2015-04-19 22:07:34

标签: android broadcastreceiver google-cloud-messaging


目前我正在创建一个支持使用GCM的用户之间聊天的应用程序,一切正常,但是当我点击通过通知收到的消息时,聊天活动将以空数据打开。
< / p>

我已在清单中注册了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>

在聊天活动中,我有这样的事情:

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

    }
}

1 个答案:

答案 0 :(得分:0)

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

的官方Google文档

另请注意这个包含聊天应用程序代码的tutorial

希望有助于!!