title: Android: Alarm not being triggered through AlarmManager
body: <p>I am writing a simple Android program that triggers an alarm 15 seconds after the application initialization (plays the default ringtone and pushes a notification) through <code>AlarmManager</code>. Below is my code:</p>
  <p><strong>MainActivity.java</strong>:</p>
  <pre><code>package com.example.basicalarmsetter;
  
  import androidx.annotation.RequiresApi;
  import androidx.appcompat.app.AppCompatActivity;
  
  import android.app.AlarmManager;
  import android.app.PendingIntent;
  import android.content.Context;
  import android.content.Intent;
  import android.os.Build;
  import android.os.Bundle;
  import android.os.SystemClock;
  
  public class MainActivity extends AppCompatActivity {
      private int uniqueId = 0;
  
      // Schedules a notification in the future given the delay
      @RequiresApi(api = Build.VERSION_CODES.O)
      private void scheduleNotification(int matchId, long delay) {
          // Construct the PendingIntent which will trigger our alarm to go off
          Intent notificationIntent = new Intent();
          notificationIntent.setAction(&quot;com.example.basicalarmsetter.MatchNotification&quot;);
  
          PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), matchId, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT) ;
          long futureInMillis = SystemClock.elapsedRealtime() + delay;
  
          // Set off our PendingIntent
          AlarmManager alarmManager = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
          alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, futureInMillis, pendingIntent);
          assert alarmManager != null;
          alarmManager.setExact(AlarmManager. ELAPSED_REALTIME_WAKEUP, futureInMillis, pendingIntent);
      }
  
      // Sets an Alarm at a future specified date
      @RequiresApi(api = Build.VERSION_CODES.O)
      private void setAlarm(long notificationDelay) {
          try {
              System.out.println(&quot;Setting alarm at &quot; + notificationDelay + &quot; seconds&quot;);
  
              // Sets off a notification after 5 seconds
              scheduleNotification(uniqueId, notificationDelay);
  
              uniqueId++;
  
          } catch (Exception ex) {
              System.out.println(&quot;Cannot print alarm!&quot;);
              System.out.println(&quot;Exception: &quot; + ex.toString());
          }
      }
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          setAlarm(15000);
      }
  }
  </code></pre>
  <p><strong>AndroidManifest.xml</strong>:</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;manifest xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      package=&quot;com.example.basicalarmsetter&quot;&gt;
  
      &lt;application
          android:allowBackup=&quot;true&quot;
          android:icon=&quot;@mipmap/ic_launcher&quot;
          android:label=&quot;@string/app_name&quot;
          android:roundIcon=&quot;@mipmap/ic_launcher_round&quot;
          android:supportsRtl=&quot;true&quot;
          android:theme=&quot;@style/AppTheme&quot;&gt;
          &lt;activity android:name=&quot;.MainActivity&quot;&gt;
              &lt;intent-filter&gt;
                  &lt;action android:name=&quot;android.intent.action.MAIN&quot; /&gt;
  
                  &lt;category android:name=&quot;android.intent.category.LAUNCHER&quot; /&gt;
              &lt;/intent-filter&gt;
          &lt;/activity&gt;
  
          &lt;receiver
              android:name=&quot;com.example.basicalarmsetter.MatchNotification&quot;
              android:enabled=&quot;true&quot;
              android:exported=&quot;true&quot;&gt;
              &lt;intent-filter&gt;
                  ...
                  &lt;action android:name=&quot;com.example.notificationtest.MatchNotification&quot; /&gt;
              &lt;/intent-filter&gt;
          &lt;/receiver&gt;
      &lt;/application&gt;
  
  &lt;/manifest&gt;
  </code></pre>
  <p><strong>MatchNotification.kt</strong>:</p>
  <pre><code>package com.example.basicalarmsetter
  
  import android.app.Notification
  import android.app.NotificationManager
  import android.content.BroadcastReceiver
  import android.content.Context
  import android.content.Intent
  import android.media.MediaPlayer
  import android.media.RingtoneManager
  import android.net.Uri
  import androidx.core.app.NotificationCompat
  
  class MatchNotification : BroadcastReceiver() {
      var NOTIFICATION_ID = &quot;notification-id&quot;
      var NOTIFICATION_CHANNEL_ID = &quot;10001&quot;;
  
      private lateinit var player: MediaPlayer;
      private lateinit var context: Context;
  
      // Construct the notification to push to the user given the teams in the match
      private fun getNotification(
          content: String
      ): Notification? {
          val builder = NotificationCompat.Builder(
              context,
              &quot;default&quot;
          )
  
          builder.setContentTitle(&quot;NBA Alarm&quot;)
          builder.setStyle(NotificationCompat.BigTextStyle().bigText(content))
          builder.setContentText(content)
          builder.setSmallIcon(R.drawable.ic_launcher_foreground)
          builder.setAutoCancel(true)
          builder.setChannelId(NOTIFICATION_CHANNEL_ID)
  
          return builder.build()
      }
  
      override fun onReceive(context: Context, intent: Intent) {
          System.out.println(&quot;Match Notification Activated.&quot;);
  
          this.context = context
  
          val notificationManager =
              context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
  
          val id = intent.getIntExtra(NOTIFICATION_ID, 0)
          notificationManager.notify(id, getNotification(&quot;Trigger Notification!&quot;))
  
          // Retrieve the URI of the alarm the user has set
          var ringtoneUri:Uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE)
          player = MediaPlayer.create(context, ringtoneUri)
  
          player.start()
      }
  
  }
  </code></pre>
  <p>This seems strange, consdering that I have specified my <code>MatchNotification</code> class as a <code>receiver</code> in my <code>AndroidManifest.xml</code> file.</p>
  <p><strong>Devices Tested On</strong>:</p>
  <ol>
  <li>Motorola Moto E6 (Android 9)</li>
  <li>Emulator for Pixel 2 (API 26)</li>
  </ol>
  <p><strong>Note: The solution should have the MainActivity code in Java</strong></p>