-1

I have to fire my notification 30 times in order to play the alarm sound 30 seconds continuously. But the process needs to get out of the loop if the user remove the notification from the notification pool? What is the best way to do this?

for(int i ; i < 30; i++) {
    notificationManager.notify(tag, NOTIFICATION_ID, notification);
    Thread.sleep(1000);
}
LEMUEL ADANE
  • 6,680
  • 14
  • 48
  • 64
  • http://stackoverflow.com/questions/15505578/detect-if-notification-has-been-deleted. I think this might help you – Rajesh Aug 28 '15 at 07:38
  • 3
    Wouldn't it make more sense to just play a long sound on the notification; http://stackoverflow.com/questions/15809399/android-notification-sound – Richard Tingle Aug 28 '15 at 07:39
  • @RichardTingle IMO, if I set a short notification sound, it could mean that I don't want it to be too long, for any reason, so I wouldn't play something that the user didn't choose (either a longer sound or the same sound 30 times). To me the notification sound should just stick to the user preference. – BackSlash Aug 28 '15 at 07:42
  • 1
    30 notifications??? what if it were play the sound one hour, would you fire 3600 notifications??? whats the idea behind it? – pskink Aug 28 '15 at 08:03
  • The idea is to play the notification sound repeatedly and stop if the notification is cancelled or clicked by the user. – LEMUEL ADANE Sep 01 '15 at 02:25
  • @RichardTingle play longer sounds means longer sound file that would mean more over head on the APK file esp. if I have several sound files. – LEMUEL ADANE Sep 01 '15 at 12:09

1 Answers1

0

This answered my question. FLAG_INSISTENT repeatedly played the sound until the user open the notification pool. No need for the 'for' loop.

// turn on the alarm
notification.sound = soundUri;
notification.flags |= Notification.FLAG_INSISTENT;
// after 30 seconds turn off the alarm
new Handler().postDelayed(
    new Runnable() {
            @Override
            public void run() {
                notification.flags ^= Notification.FLAG_INSISTENT;
                notificationManager.notify(tag, NOTIFICATION_ID,   
                    notification);
            }
    }, 30000);
LEMUEL ADANE
  • 6,680
  • 14
  • 48
  • 64