Basically, I am trying to build an alarm app which has some buttons with some predefined Date and Time. I have tried using AlarmManager and broadcast receiver in the first place but didn't work. So, I used foreground service with alarmManager but still, the alarm doesn't fire when the app is destroyed. I am a newbie. I tried searching the internet but I had no luck. Hope there is a lot of people here to help me out. Thanks in Advance. 

Here I am just trying to set only one alarm for testing. Otherwise, I am using a variable as request code for multiple alarms.

AndroidManifest.xml

```
 <receiver android:name=".AlarmReceiver" />

    <activity
        android:name=".Activity.PlayerDetailsActivity"
        android:theme="@style/AppTheme.NoActionBar"></activity>
    <activity android:name=".Activity.FixtureActivity" />

    <service android:name=".MyService"/>

```

MyService.java

```
public class MyService extends Service {
public MyService() {
}

@Override
public IBinder onBind(Intent intent) {
    // TODO: Return the communication channel to the service.
    throw new UnsupportedOperationException("Not yet implemented");
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    Toast.makeText(this, "Started", Toast.LENGTH_SHORT).show();
    Log.e("service","service");
    long longExtra = intent.getLongExtra(Constants.ALARM_TIME, 0000);

    //Testing Area Start
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(longExtra);

    int mMin = calendar.get(Calendar.MINUTE);
    int mMonth = calendar.get(Calendar.MONTH);
    int mDay = calendar.get(Calendar.DAY_OF_MONTH);
    int mHour = calendar.get(Calendar.HOUR_OF_DAY);

    Log.e("hour min month day"," "+mHour + " : "+mMin+" month : "+mMonth+" "+" Date : "+mDay+" ");
    String currentDateTime=getDeviceDateTime();
    Log.e("CurrentdateTime",""+currentDateTime);

    Log.e("longExtra",""+longExtra);
    //Testing Area End

        String CHANNEL_ID = "my_channel_01";
    NotificationChannel channel = null;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        channel = new NotificationChannel(CHANNEL_ID,
                "Channel human readable title",
                NotificationManager.IMPORTANCE_DEFAULT);

    ((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)).createNotificationChannel(channel);

        Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
                .setContentTitle("dfdf")
                .setSmallIcon(R.drawable.ic_notifications_black_24dp)
                .setContentText("dfdfd").build();
        startForeground(3, notification);
    }

    /*Intent alertIntent = new Intent(getApplicationContext(), AlarmReceiver.class);
    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

    Log.d("I",""+longExtra);
    alarmManager.set(AlarmManager.RTC_WAKEUP, 6000000, PendingIntent.getBroadcast(getApplicationContext(), 0, alertIntent,
            PendingIntent.FLAG_ONE_SHOT));*/

    AlarmManager manager= (AlarmManager)getSystemService(ALARM_SERVICE);
    Intent myIntent;
    myIntent = new Intent(getApplicationContext(),AlarmReceiver.class);
    myIntent.putExtra("check",true);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this,0,myIntent,0);
    Long finalTime =longExtra-System.currentTimeMillis();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        manager.setExact(AlarmManager.RTC_WAKEUP,longExtra,pendingIntent);
    }
    else
        manager.set(AlarmManager.RTC_WAKEUP,longExtra,pendingIntent);

    return START_NOT_STICKY;
}

@Override
public void onDestroy() {
    super.onDestroy();
}
}

```

Alarmreceiver.java

```
 public class AlarmReceiver extends BroadcastReceiver
{
public static final String CHANNEL_ID = "47";
@Override
public void onReceive(Context context, Intent intent)
{
    intent = new Intent(context, SplashActivity.class);
    intent.putExtra("not",true);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);

    Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    NotificationCompat.Builder notificationCompat = new NotificationCompat.Builder(context,CHANNEL_ID);
    notificationCompat.setSmallIcon(R.drawable.ic_notifications_black_24dp);
    notificationCompat.setContentTitle("My Noticiation");
    notificationCompat.setContentText(getPreferences(context).getDateTime());
    notificationCompat.setContentIntent(pendingIntent);
    notificationCompat.setSound(alarmSound);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

        int importance = NotificationManager.IMPORTANCE_DEFAULT;
        NotificationChannel channel = new NotificationChannel(CHANNEL_ID, "channel name", importance);
        // Register the channel with the system; you can't change the importance
        // or other notification behaviors after this
        NotificationManager notificationManager = context.getSystemService(NotificationManager.class);
        notificationManager.createNotificationChannel(channel);
    }

    Notification notification = notificationCompat.build();

    NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
    notificationManager.notify(100,notification);
    Toast.makeText(context, "Alarm Working", Toast.LENGTH_SHORT).show();
}
}

```