If I remember correctly, and it has been a long time since I read the books, When tolkien is describing what a hobbit is to the reader at the start of The Hobbit he describes them as a forgotten or extinct race in an age long past. Implying that the story we are about to read is indeed part of are own pre-history.
So of course in this world if somebody were to use gunpowder it would 'work', as it does in our own world.
Now I understand your question of why wouldn't Gandalf use gunpowder to defend the gates of Minus Tirith against Saurons approaching armies if he had access to such a powerful weapon.
Well the simplest answer is that he didn't have a powerful weapon. Now it has been a while since I have read the books, but I believe they mention that Gandalf made fireworks. This could easily be just a convenient description. In which Tolkien was merely tring to say that whatever magic Gandalf employed it had the effect or final result which would equate to what we would call fireworks. This is allows the reader to easily and immediately recognize what the author means.
It is much easier for an author such as Tolkien to simply say 'fireworks' rather than give a complete and detailed analysis of the particular magic or rituals involved. Just as when describing hobbits he said that they were like humans but they live longer, are shorter and have fuzzy leathery feet rather than describing them from the ground up. Just as if I were to time travel 800 years into the past I might describe a car as a horseless carriage rather than try to attempt to explain to the villagers the inner workings of a combustion engine.
Now this may or may not have been the case, perhaps my memory has faded and he does specfically mention gunpowder or it is indeed inferred that they are fireworks as we know them with only a few cosmetic magical touches rather than simply an equivalent.
But I think that we can give Tolkien some leeway on this matter.
As for the point of not thinking of using it as a weapon?
That is entirely possible too.
It took the chinese one thousand years, why not Gandalf? Especially in this case; if he was indeed the only one with the knowledge of how to make them, and it seems that this could very well be the case. And if he didn't think of them as having any other value other than for entertainments sake then how likely would it be that he would pass the information on? Not very likely, especially if it required his magic to give them the best effect. With only one mind to contemplate the matter it would be quite likely that a military use would not be thought of. Especially if observers attributed the whole affair as magicians work that could be only be performed by wizards.
I wouldn't press the matter too much in any case. If Tolkien meant to have gunpowder in middle earth then he would have put it in. Unfortuneately he is dead so we will never know if he made a slip or not.
I am currently taking a Classical studies course on Greek & Roman Epics. Reading the Aneid I found that Virgil mentions Sarapedons body lying in Troy. While in the Illiad there is agreement made between Hera and Zeus that Sarapedons body would be moved to his homeland (magically) after his death.
Was this intentional or was it a slip by virgil, or by one of the translators? We will never know.
While this is not the best example. Whenever people attempt to read into one of the classical poems as you have with Tolkien Gary, my teacher usually has this reply:
Perhaps, but if the author truly wanted to get that across it would have played a much more significant role in the story.
So if those are real fireworks or they were accessible to all and readily made then Tolkien would have most likely included that in the stories. And spent a lot more time dealing with the topic.
Because nobody uses gunpowder in the books it is pretty safe to assume that:
A) There was no gunpowder just magical fireworks or,
B) That nobody had thought of using gunpowder agressively or,
C) That Tolkien wasn't thinking clearly when had Gandalf use fireworks for celebrations, and failed to see the implications.
P.S.
I am surprised that you never noticed this before. I noticed it the first time I read the books, but dismissed it for the third reason. If it is not in the book then it is not in the book. This rule works 99.9% of the time.