Refactor sendNotification

add flag checking before waiting to avoid waiting forever which caused
by bad timing issue