'Particular activity is not opening when app is in background or killed?

I want to open the NotificationActivity of my application when AppReceiver calls and triggered the onReceive method.

I have 2 Activities named MainActivity and NotificationActivity in my MainActivity there is a click event of a button when I click on it I have sent a call to my AppReceiver class and I get the call also but in my onReceive method I have written a Toast and an Intent to start my NotificationActivity class but it is not opening when my app is in background.

Here is my MainActivity class :

import static 
com.example.fullscreennotificationtest.NotificationActivity.FULL_SCREEN_ACTION;
import static 
com.example.fullscreennotificationtest.NotificationActivity.NOTIFICATION_ID;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationManagerCompat;

public class MainActivity extends AppCompatActivity {

Button btn_click_me;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    btn_click_me = findViewById(R.id.btn_click_me);

    btn_click_me.setOnClickListener(v -> {
        Intent intent = new Intent(FULL_SCREEN_ACTION, null, this, AppReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        AlarmManager alarmManager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
        if (alarmManager != null) {
            alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 50, pendingIntent);
        }

        NotificationManagerCompat.from(this).cancel(NOTIFICATION_ID);
    });
}
}

and here is my AppReceiver class :

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;

public class AppReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    Log.i("TAG", "onReceive: "+intent.getAction());
    if (NotificationActivity.FULL_SCREEN_ACTION.equals(intent.getAction())) {
        Toast.makeText(context, "Called", Toast.LENGTH_SHORT).show();
        Intent intent1 = new Intent(context,NotificationActivity.class);
        intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent1);
    }
}
}

Here is my NotificationActivity class :

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;

import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.WindowManager;

public class NotificationActivity extends AppCompatActivity {

private static final String CHANNEL_ID = "my_channel";
static final String FULL_SCREEN_ACTION = "full_screen_action";
static final int NOTIFICATION_ID = 1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_notification);
    

    //set flags so activity is showed when phone is off (on lock screen)
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
            | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
            | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

}
}

Here is my Menifest file :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.fullscreennotificationtest">


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

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/Theme.FullScreenNotificationTest">
    <activity
        android:name=".NotificationActivity"
        android:configChanges="orientation|screenSize|keyboardHidden"
        android:excludeFromRecents="true"
        android:exported="false"
        android:hardwareAccelerated="true"
        android:screenOrientation="portrait"/>
    <activity
        android:name=".MainActivity"
        android:exported="true"
        android:screenOrientation="portrait">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <receiver
        android:exported="false"
        android:name=".AppReceiver"
        android:excludeFromRecents="true"
        android:hardwareAccelerated="true"/>
</application>


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source