A novel Schiff base derivative HMA containing dicyanomethylene-4H-pyran unit was designed and synthesized. It can be used to detect Fe3+, Al3+, Cr3+ accompanied with near-infrared (NIR) fluorescence enhancement. Fluorescence experiments show that it has a large Stokes shift (λex = 480 nm, λem = 670 nm), low detection limit and large binding constant. The interaction between HMA and Fe3+, Al3+, Cr3+ was investigated by NMR, FT-IR, as well as density functional theoretical calculations. It shows that the emission of HMA experiences an excited state intramolecular proton transfer (ESIPT) process, while the addition of Fe3+, Al3+ and Cr3+causes their coordination, increases the rigidity of HMA and enhances the fluorescence. Additionally, the reversible experiment was carried out by adding F− ion. For Al3+ and Cr3+, the corresponding solutions can return to their original state but not for Fe3+. On the basis of the reversible experiment, a molecular logic gate was designed with Al3+/Cr3+ and F− as input and the fluorescence signal at 670 nm of HMA as output.