androidservicealarmmanagerlocalnotification

AlarmManager notification not triggering after killing application


Need to show notification as remainder on particular date and time. To set remainder I am using AlarmManger to set task and using broadcast receiver I am showing notification. But after killing application Notification remainder is not triggering.

To set date and time am using this code

 Intent notificationIntent = new Intent(this, AddAppointmentAlarmReceiver.class);
        notificationIntent.putExtra(NOTIFICATION,addAppointDataBinding.edTitle.getText().toString());
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(date);
        calendar.set(Calendar.HOUR,7);
        calendar.set(Calendar.MINUTE,0);
        calendar.set(Calendar.SECOND,0);
        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {

            alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
        } else {

            alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
        }

Service class

    public class AlarmIntentService extends Service {
    private static final int NOTIFICATION_ID = 3;
    Context context;
    public AlarmIntentService(Context applicationContext) {
        super();
        this.context = applicationContext;
    }

    public AlarmIntentService() {
    }
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        super.onStartCommand(intent, flags, startId);


        NotificationManagerCompat managerCompat = NotificationManagerCompat.from(this);
        NotificationChannel channel = null;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            channel = new NotificationChannel(Constants.NOTIFICATION_ID,intent.getStringExtra(NOTIFICATION), NotificationManager.IMPORTANCE_HIGH);
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            managerCompat.createNotificationChannel(channel);
        }
        Intent notifyIntent = new Intent(this, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 2, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        //to be able to launch your activity from the notification
        Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
        Notification.Builder builder = new Notification.Builder(this);
        builder.setContentTitle(NOTIFICATION_TITLE);
        builder.setContentText(intent.getStringExtra(NOTIFICATION));
        builder.setSmallIcon(R.mipmap.ic_launcher);
        builder.setContentIntent(pendingIntent);

        builder.setSound(alarmSound);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            builder.setChannelId(Constants.NOTIFICATION_ID);
        }
        Notification notificationCompat = builder.build();
        managerCompat.notify(NOTIFICATION_ID, notificationCompat);

        return START_STICKY;
    }



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

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

}

Broadcast receiver class

public class AddAppointmentAlarmReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        AlarmWakeLock.acquire(context);

        Intent intent1 = new Intent(context, AlarmIntentService.class);
        intent1.putExtra(NOTIFICATION,intent.getStringExtra(NOTIFICATION));
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            context.startForegroundService(intent1);
        }else {
            context.startService(intent1);
        }

    }
}

Solution

  • I remember having similar issues with Alarms and how it worked when it comes to triggering. Please try piece of code below and see if you get any results.

    // Depending on the version of Android use different function for setting an Alarm.
    // setAlarmClock() => Android < Marshmallow
    // setExactAndAllowWhileIdle() => Android >= Marshmallow
            if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.M) {
                alarmManager.setAlarmClock(
                    AlarmManager.AlarmClockInfo(alarmTimeAtUTC, pendingIntent),
                    pendingIntent
                )
            } else {
                alarmManager.setExactAndAllowWhileIdle(
                    AlarmManager.RTC_WAKEUP,
                    alarmTimeAtUTC,
                    pendingIntent
                )
            }
    

    Step 1: Trying without AlarmIntentService:

    In your setAlarm function do the following:

    ...
    Intent notificationIntent = new Intent(this, AddAppointmentAlarmReceiver.class);
    notificationIntent.setAction = "AppointmentBroadcastReceiverAction"
    ...
    

    Then, in your Broadcast Receiver:

    public class AddAppointmentAlarmReceiver extends BroadcastReceiver {
    
        @Override
        public void onReceive(Context context, Intent intent) {
            if(intent.action == "AppointmentBroadcastReceiverAction") {
                sendNotification()
            }
        }
    }
    

    Step 2: Building Max Priority Notification: [Sample Code in Kotlin; Adapt to Java]

    private fun sendInternetDaysLeftNotification(context: Context) {
            val channelId: String = context.resources.getString(R.string.internetDaysLeftNotificationsChannelId)
            val defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
            val notificationBuilder: NotificationCompat.Builder = NotificationCompat.Builder(context, channelId)
                .setSmallIcon(R.drawable.ic_notification)
                .setContentTitle(%YOUR TITLE%)
                .setContentText(%YOUR TEXT%)
                .setAutoCancel(true)
                .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
                .setPriority(NotificationCompat.PRIORITY_MAX)
    
            val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                val channel = NotificationChannel(channelId, DEFAULT_NOTIFICATION_CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH)
                notificationManager.createNotificationChannel(channel)
            }
    // Random.nextInt() can be replaced with any Integer.
                notificationManager.notify(Random.nextInt(0, 100), notificationBuilder.build())
            }