【问题标题】:android GCM Receive Messages from Multiple devicesandroid GCM 从多个设备接收消息
【发布时间】:2013-07-03 09:37:29
【问题描述】:

我正在 GoogleCloudMessaging(GCM) 的帮助下构建一个聊天应用程序。我能够发送和接收消息。我的问题是当我同时从多个设备接收消息时,所有这些消息都附加到我的广播接收器类中的同一个列表视图中。如何根据发件人分离消息并将当前聊天消息附加到列表视图。并根据发件人为其他消息发出单独的通知,当我单击通知时,它应该根据发件人打开与消息相同的列表视图。

谁能给我一种有效的方法。如果您有任何示例代码来处理这种情况,请发布代码。

我当前的代码:

 public class Serious extends BroadcastReceiver {


    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
         String action=intent.getAction();
         if (action.equals("com.google.android.c2dm.intent.REGISTRATION"))
         {
             String registrationID=intent.getStringExtra("registration_id");
            // Log.i("uo",registrationID);
             String error=intent.getStringExtra("error");
             String unregisterd=intent.getStringExtra("unregistered");

         }
         else if(action.equals("com.google.android.c2dm.intent.RECEIVE"))
         {
             String data1=intent.getStringExtra("data1");
             String data2=intent.getStringExtra("data2");
             addNewMessage(new Message(data2, false));

            /* PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
                        new Intent(context, MainActivity.class), 0);

                NotificationCompat.Builder mBuilder =
                        new NotificationCompat.Builder(context)
                .setSmallIcon(R.drawable.ic_launcher)
                        .setContentTitle("My Notification")
                        .setDefaults(Notification.DEFAULT_SOUND)
                        .setAutoCancel(true)
                        .setContentText(data1+data2);
                mBuilder.setContentIntent(contentIntent);


                NotificationManager mNotificationManager =
                    (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
                mNotificationManager.notify(1, mBuilder.build());*/



         }

    }
     void addNewMessage(Message m)
        {
            MainActivity.messages.add(m);
            MainActivity.adapter.notifyDataSetChanged();
            //MainActivity.getListView().setSelection(MainActivity.messages.size()-1);
        }
}

【问题讨论】:

    标签: broadcastreceiver google-cloud-messaging


    【解决方案1】:

    您应该在您的 gcm 消息中传递一个包含发件人 ID 的参数。然后,当您处理到达的消息时,使用该发件人 ID 来决定在哪里添加该消息。为了显示多个通知,将不同的 int 值传递给 notify。目前您总是通过 1,因此新通知会覆盖旧通知。

    【讨论】:

    • 我需要将消息存储在移动数据库中以维护堆栈,否则 gcm 中是否会有任何 api 通过传递registrationid 来检索消息
    • 如果我从不同的registrationid收到不同的消息,我是否可以从registrationid列表中显示特定registrationid的通知,如果可以,请提供示例代码。如果有多个用户的完整聊天应用程序发布该项目或链接到我,以便我可以了解一次处理来自多个注册ID(朋友)的数据。请为此主题提供一些帮助
    • @madanV 您必须将消息存储在移动数据库中,或者在应用程序启动时从您的服务器加载它们。 GCM 中没有用于检索旧消息的 API(它们在传递后不会存储在 GCM 服务器中)。
    • 感谢您的回复您是否有任何项目的工作样本。如何将我的移动联系人与类似于 whatsapp 的服务器数据库同步。
    猜你喜欢
    • 1970-01-01
    • 2014-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多