So the thing is, the blockade of Gaza (let alone the occupation that has been going on since 1967) is an act of war. When you commit an act of war, you get attacked. If Israel wanted Hamas to not attack them, they'd stop their occupation of Gaza (and the rest of Palestine).
Also about the ceasefire part, If Israel had kept its ceasefires we wouldn't have this mess.