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