What Zlurpo said. Create a chamber for the magnet, and have the magnet pull towards its mate in the direction of that support, with the aluminum or what-have-you gripping the foam. It also has the added benefit of making it easy to slide the magnet off before pulling it away, reducing the stress on the glue. You have to be careful with the strength, because they can easily tear through foam. I'd recommend against having it pull towards the foam, because the pressure of its pull will deform your foam, even if it doesn't rip its way out.