title: My Android app is unable to run in the background
body: <p>I was learning about alarms. I created an app where clicking on button would send you a notification after 5 seconds. The app works fine when it is on screen. But it does not work when my app goes to background after pressing the button. The notification which was supposed to come, comes only after I open my app</p>
  <p>I have tried changing the setting of my Samsung phone by changing the option &quot;Putting unused app to sleep &quot;  to false in settings. But still it did not work.</p>
  <p>is there something wrong with my code? or do I need to use some sort of permissions?</p>
  <p>Code:
  activity_main.xml:</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;LinearLayout  xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
      xmlns:tools=&quot;http://schemas.android.com/tools&quot;
      android:layout_width=&quot;match_parent&quot;
      android:layout_height=&quot;match_parent&quot;
      android:gravity=&quot;center&quot;
      tools:context=&quot;.MainActivity&quot;&gt;
  
      &lt;Button
          android:layout_width=&quot;wrap_content&quot;
          android:layout_height=&quot;wrap_content&quot;
          android:text=&quot;Set Alarm!&quot;
          android:id=&quot;@+id/btn&quot;
         /&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  <p>MainActivity.kt:</p>
  <pre><code>package com.example.alarms1
  
  import android.app.*
  import android.content.Intent
  import android.net.Uri
  import android.os.Build
  import androidx.appcompat.app.AppCompatActivity
  import android.os.Bundle
  import android.os.PowerManager
  import android.os.SystemClock
  import android.widget.Toast
  import androidx.core.app.NotificationCompat
  import kotlinx.android.synthetic.main.activity_main.*
  
  class MainActivity : AppCompatActivity() {
      override fun onCreate(savedInstanceState: Bundle?) {
          super.onCreate(savedInstanceState)
          setContentView(R.layout.activity_main)
          if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.M) {
              var packageName = this .getPackageName();
              var pm = getSystemService(POWER_SERVICE) as PowerManager;
              if (!pm.isIgnoringBatteryOptimizations(packageName)) {
                  var intent =Intent();
                  intent.setAction(android.provider.Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
                  intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                  intent.setData(Uri.parse(&quot;package:&quot; + packageName));
                  startActivity(intent);
              }
          }
          Toast.makeText(this,&quot;Hello&quot;,Toast.LENGTH_SHORT).show();
          btn.setOnClickListener {
  
              var int=Intent(this,MainActivity2::class.java)
              var pi=PendingIntent.getActivity(this,123,int,PendingIntent.FLAG_ONE_SHOT);
              var myAlarmm=getSystemService(ALARM_SERVICE) as AlarmManager;
              myAlarmm.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,SystemClock.elapsedRealtime()+5000,pi);
          }
      }
  }
  </code></pre>
  <p>activity_main2.xml:</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
      xmlns:tools=&quot;http://schemas.android.com/tools&quot;
      android:layout_width=&quot;match_parent&quot;
      android:layout_height=&quot;match_parent&quot;
      tools:context=&quot;.MainActivity2&quot;&gt;
  
  &lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;
  </code></pre>
  <p>MainActivity2.kt:</p>
  <pre><code>package com.example.alarms1
  
  import android.app.NotificationChannel
  import android.app.NotificationManager
  import android.os.Build
  import androidx.appcompat.app.AppCompatActivity
  import android.os.Bundle
  import androidx.core.app.NotificationCompat
  
  class MainActivity2 : AppCompatActivity() {
      override fun onCreate(savedInstanceState: Bundle?) {
          super.onCreate(savedInstanceState)
          setContentView(R.layout.activity_main2)
  
          var nm=getSystemService(NOTIFICATION_SERVICE) as NotificationManager
          if(Build.VERSION.SDK_INT&gt;= Build.VERSION_CODES.O)
              nm.createNotificationChannel(
                  NotificationChannel(&quot;Channel1&quot;,&quot;Name1&quot;,
                      NotificationManager.IMPORTANCE_DEFAULT)
              )
  
          var myNotification= NotificationCompat.Builder(this,&quot;Channel1&quot;)
              .setSmallIcon(R.drawable.ic_launcher_background)
              .setContentTitle(&quot;Hello World&quot;)
              .setContentText(&quot;Hello namaste stasriaka&quot;).build()
          nm.notify(123,myNotification);
  
          finish();
      }
  }
  </code></pre>
  <p>Also I have made my activity_main2 to be transparent which works fine</p>