Yes, Gander knew what he was doing, because he knew that he was in a dangerous situation. Dogs can be very protective of their owners, and Gander knew that he was protecting his soldiers when he ran towards the enemy with the grenade. The dog had plenty of time to train with the troops and he knew the important job that he was doing in battle.
When Gander, the Dickin-Medal-winning dog sacrificed himself in the battle of Hong Kong, it is my belief that he knew that he was attacking the enemy. I believe that the perception that the entire animal kingdom has is very in-tune and very accurate. I also believe that he would do it again.
The dog was unlikely to understand that he was attacking an enemy when he sacrificed himself. However, that doesn't mean he didn't know what he was doing at all; dogs love fiercely and protect their pack or their handler. He may have understood that he was protecting his master or his pack, and the bravery was very real.
Although I believe that Gander was a loyal dog who was obeying his masters, I do not think that he had any knowledge that he was attacking the enemy. Gander was, simply, following instructions, which, unfortunately, led to his death. It is unfortunate that this occurred, but there was no way that anyone could have predicted that it was going to happen.