Android app – play (notification) sound every hour, even when the app is not in the foreground


How can my app play a short sound every hour, even when it is not running in the foreground?

Try-1: I tried a handler, but this does not help.

Try-2: as @Marcin, suggested, I tried the AlarmManager. No sound is played when the mobile is inactive. When I open the app, then it plays the sound.

Try-3: Add a notification channel with high priority on it. This plays the sound at least once, even when the mobile is standby. Swiping the notification away won’t play the sound again. Setting the notification autoCancel to false won’t help.

private void setAlarm() {
    alarmManager = (AlarmManager) getSystemService( Context.ALARM_SERVICE);
    Intent intent = new Intent( this, AlarmReceiver.class);
    pendingIntent = PendingIntent.getBroadcast( this, 0, intent, FLAG_IMMUTABLE);
    alarmManager.setInexactRepeating( AlarmManager.RTC_WAKEUP,
            60000L, pendingIntent);

private void createNotificationChannel() {
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        CharSequence name = "My ReminderChannel";
        String description = "Channel for the Alarm manager";
        int importance = NotificationManager.IMPORTANCE_HIGH;
        NotificationChannel channel = new NotificationChannel( "myandroid", name, importance);
        channel.setDescription( description);
        NotificationManager notificationManager = getSystemService( NotificationManager.class);
        notificationManager.createNotificationChannel( channel);

The AlarmReceiver is:

public class AlarmReceiver extends BroadcastReceiver {
    public void onReceive(Context context, Intent intent) {
        Intent i = new Intent( context, MainActivity.class);
        intent.setFlags( Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        PendingIntent pendingIntent = PendingIntent.getActivity( context, 0, i, PendingIntent.FLAG_IMMUTABLE);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(context, "myandroid")
                .setContentIntent( pendingIntent);
        NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(context);

    public void playRingtone() {
        try {
            MediaPlayer mp = MediaPlayer.create(MainActivity.mainActivity, R.raw.tock);
        } catch (Exception e) {

In the AndroidManifest.xml is:

<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM"/>

    <receiver android:name="com.example.myapp.AlarmReceiver"/>

Of course I don’t want my silent app draining my battery.


Use workmanager.
here is the link.

Answered By – Saiful Sazib

Answer Checked By – Mildred Charles (FlutterFixes Admin)

Leave a Reply

Your email address will not be published.