// call on button click
scheduleNotification(getNotification(et_comments.getText().toString()), different );
// instead of delay i use different (different means time difference between next_action_date and current date).
private void scheduleNotification(Notification notification, long different) {
Intent notificationIntent = new Intent( this, AlarmReceiver. class ) ;
notificationIntent.putExtra(AlarmReceiver. NOTIFICATION_ID , 1 ) ;
notificationIntent.putExtra(AlarmReceiver. NOTIFICATION , notification) ;
PendingIntent pendingIntent = PendingIntent. getBroadcast ( this, 0 , notificationIntent , PendingIntent. FLAG_UPDATE_CURRENT ) ;
/* long futureInMillis = different;
System.out.println("futureInMillis " + futureInMillis);*/
long futureInMillis = SystemClock.elapsedRealtimeNanos () + different ;
System.out.println("futureInMillis " + futureInMillis);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context. ALARM_SERVICE ) ;
assert alarmManager != null;
alarmManager.set(AlarmManager. ELAPSED_REALTIME_WAKEUP , futureInMillis , pendingIntent) ;
}
private Notification getNotification (String content) {
NotificationCompat.Builder builder = new NotificationCompat.Builder( this, default_notification_channel_id ) ;
builder.setContentTitle( "Scheduled Notification" ) ;
builder.setContentText(content) ;
builder.setSmallIcon(R.drawable. ic_launcher_foreground ) ;
builder.setAutoCancel( true ) ;
builder.setChannelId( NOTIFICATION_CHANNEL_ID ) ;
return builder.build() ;
}
// AlarmReceiver.java
public class AlarmReceiver extends BroadcastReceiver {
public static String NOTIFICATION_ID = "notification-id" ;
public static String NOTIFICATION = "notification" ;
public void onReceive (Context context , Intent intent) {
NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context. NOTIFICATION_SERVICE ) ;
Notification notification = intent.getParcelableExtra( NOTIFICATION ) ;
if (android.os.Build.VERSION. SDK_INT >= android.os.Build.VERSION_CODES. O ) {
int importance = NotificationManager. IMPORTANCE_HIGH ;
NotificationChannel notificationChannel = new NotificationChannel( NOTIFICATION_CHANNEL_ID , "NOTIFICATION_CHANNEL_NAME" , importance) ;
assert notificationManager != null;
notificationManager.createNotificationChannel(notificationChannel) ;
}
int id = intent.getIntExtra( NOTIFICATION_ID , 0 ) ;
assert notificationManager != null;
notificationManager.notify(id , notification) ;
}
}
// Using this code I get the instant notification. what should I do please help!!!