Reputation: 345
I am working on a chat application and trying to use Google cloud messaging following this 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);
}
Upvotes: 1
Views: 175
Reputation: 2774
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.
Upvotes: 1
Reputation: 185
Not eligible to comment so has to post this in an answer. Its good that you are trying to use GCM server instead having your own server do the work. But if you have time, check out the socket.io library for android. You would need to understand node.js for that too. Just a suggestion for making a better app.
Upvotes: 0