Android GCM消息重复 [英] Android GCM messages repeated

查看:128
本文介绍了Android GCM消息重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个聊天应用程序,并试图在本教程中使用Google云消息



http://hmkcode.com/android-google-cloud-messaging-tutorial/

它可以正常工作,但是通知不止一次发送,
ie:如果发送hello六个通知说你好!
最少2个通知。

onHandleIntent方法:

  protected void onHandleIntent(Intent intent){
Bundle extras = intent.getExtras();

GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);

字符串messageType = gcm.getMessageType(intent);

mes = extras.getString(content);

if(mes == null)
return;

db = new AppDatabase(this);

布尔isforeground = isForeground(packageName);

MessageData data = new MessageData();
data.setTitle(extras.getString(title));
data.setContent(extras.getString(content));

data.setSend_user_name(extras.getString(send_user_name));


SimpleDateFormat sdf = new SimpleDateFormat(dd / MM / yyyy HH:mm:ss);
String currentDateandTime = sdf.format(new Date());

data.setDate(currentDateandTime);


if(isforeground){

字符串activity_group_id =整数
.toString(MessageActivity.group_id);

if(extras.getString(group_id)。equals(activity_group_id)){

MessageHandler.messageView.addMsg(data);
} else {
showToast(data);
}

} else {
showToast(data);

$ b Log.i(GCM,
Received:(+ messageType +)
+ extras.getString(title)) ;

GcmBroadcastReceiver.completeWakefulIntent(intent);

}

这里是isForeground方法

  public boolean isForeground(String myPackage){
ActivityManager manager =(ActivityManager)getSystemService(ACTIVITY_SERVICE);
List< ActivityManager.RunningTaskInfo> runningTaskInfo = manager
.getRunningTasks(1);
ComponentName componentInfo = runningTaskInfo.get(0).topActivity;

return componentInfo.getClassName()。equals(myPackage);


解决方案

请检查以下官方GCM线程,它定义了Google方面的一些错误。



https://groups.google.com/forum/#!topic/android-gcm/EHZUTEJMeyw



现在作为一种解决方案,您可以做的是,随着来自服务器的GCM响应,传递一些随机的唯一ID,并且仅在ID是唯一的时处理GCM消息,因为该ID也将被重复,即GCM消息被复制。


I am working on a chat application and trying to use Google cloud messaging following this tutorial:

http://hmkcode.com/android-google-cloud-messaging-tutorial/

It works fine but the notifications are delivered more than once, ie: if I send "hello" there will be like six notifications saying "hello" !! The minimum is 2 notifications.

onHandleIntent method :

protected void onHandleIntent(Intent intent) {
    Bundle extras = intent.getExtras();

GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);

String messageType = gcm.getMessageType(intent);

mes = extras.getString("content");

if(mes==null)
    return;

db=new AppDatabase(this);

boolean isforeground = isForeground("packageName");

MessageData data = new MessageData();
data.setTitle(extras.getString("title"));
data.setContent(extras.getString("content"));

data.setSend_user_name(extras.getString("send_user_name"));


SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
String currentDateandTime = sdf.format(new Date());

data.setDate(currentDateandTime);


if (isforeground) {

    String activity_group_id = Integer
            .toString(MessageActivity.group_id);

    if (extras.getString("group_id").equals(activity_group_id)) {

        MessageHandler.messageView.addMsg(data);
    } else {
        showToast(data);
    }

} else {
    showToast(data);
}

Log.i("GCM",
        "Received : (" + messageType + ")  "
                + extras.getString("title"));

GcmBroadcastReceiver.completeWakefulIntent(intent);

}

here is code of isForeground method

public boolean isForeground(String myPackage) {
    ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
    List<ActivityManager.RunningTaskInfo> runningTaskInfo = manager
            .getRunningTasks(1);
    ComponentName componentInfo = runningTaskInfo.get(0).topActivity;

    return componentInfo.getClassName().equals(myPackage);
}

解决方案

Please check the following official GCM thread, which defines that there is some bug from Google side.

https://groups.google.com/forum/#!topic/android-gcm/EHZUTEJMeyw

For now as a solution, what you can do is that along with the GCM response from server, pass some random unique ID and handle the GCM message only if ID is unique, as this id also would be duplicated id that GCM message is duplicated.

这篇关于Android GCM消息重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆