Let me add some workaround you can find more tutorial outside..
First create receiver class extends BroadcastReceiver
.
public class ReminderReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
int Request_Code = intent.getExtras().getInt("TIME",0);
showNotification(context, MainActivity.class,
"New Notification Alert..!", "scheduled for " + Request_Code + " seconds",Request_Code);
}
public void showNotification(Context context, Class<?> cls, String title, String content,int RequestCode)
{
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Intent notificationIntent = new Intent(context, cls);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(cls);
stackBuilder.addNextIntent(notificationIntent);
PendingIntent pendingIntent = stackBuilder.getPendingIntent(
RequestCode,PendingIntent.FLAG_ONE_SHOT);
NotificationManager notificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = context.getString(R.string.channel_name);
String description = context.getString(R.string.channel_description);
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel("my_channel_01", name, importance);
channel.setDescription(description);
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
notificationManager.createNotificationChannel(channel);
}
NotificationCompat.Builder builder = new NotificationCompat.Builder(context,"my_channel_01");
Notification notification = builder.setContentTitle(title)
.setContentText(content).setAutoCancel(true)
.setSound(alarmSound).setSmallIcon(R.drawable.ic_launcher_background)
.setContentIntent(pendingIntent).build();
notificationManager.notify(RequestCode,notification);
}
}
Declare receiver in manifest class below activity tag..
<receiver android:enabled="true" android:name=".ReminderReceiver"/>
Then set reminder to alarm manager.
public void setReminder(Context context,Class<?> cls,int sec)
{
Intent intent = new Intent(context, cls);
intent.putExtra("TIME",sec);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, sec, intent,
PendingIntent.FLAG_ONE_SHOT);/* Find more about flags: https://developer.android.com/reference/android/app/PendingIntent */
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
am.set( AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (sec * 1000), pendingIntent );//Add time in milliseconds. if you want to minute or hour mutiply by 60.. For ex: You want to trigger 5 Min then here you need to change 5 * 60 * 1000
}
Finally set your reminder
setReminder(_Context,ReminderReceiver.class,time);
Updated
For support android version 8.0 and above you have to create notification channel. Find more here Manage Channels
Add this in above code:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = context.getString(R.string.channel_name);
String description = context.getString(R.string.channel_description);
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel("my_channel_01", name, importance);
channel.setDescription(description);
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
notificationManager.createNotificationChannel(channel);
}
Note use drawable for small icons not use mipmap or adaptive icons.Android Oreo Notification Crashes System UI
To Cancel the scheduled notification
public void cancelReminder(Context context,Class<?> cls)
{
Intent intent1 = new Intent(context, cls);
intent1.putExtra("TIME",time);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
time, intent1, PendingIntent.FLAG_ONE_SHOT);
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
if(pendingIntent != null) {
am.cancel(pendingIntent);
}
}
And use above method to delete
cancelReminder(_Context,ReminderReceiver.class);
Note: _Context should be same as used in setreminder()
method