-4

// 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!!!

Shital
  • 1
  • 3

1 Answers1

0

Unlike iOS, in Android you cannot schedule notifications directly using the OS Notifications API. Instead, you need to:
1 - Create an Alarm using the AlarmManager service.
2 - Handle the intent once Android OS fires it using a BroadcastReceiver.
3 - Show the notification using code similar to the one you posted in the question.

From your code, I am assuming that you are trying to use the method setWhen to establish when the notification should be shown. However, from the docs you can see that this method:

 /**
     * Set the time that the event occurred.  Notifications in the panel are
     * sorted by this time.
     */

Meaning that this information is only used to determine the order of the notifications, but it does not "schedule" the notification in any way.

There are several tuts out there regarding how to program this feature. Take a look at this one for instance:

https://www.raywenderlich.com/1214490-android-notifications-tutorial-getting-started

edit:

From the code you have posted. Be careful with this line:

long futureInMillis = SystemClock.elapsedRealtimeNanos () + different

The call SystemClock.elapsedRealtimeNanos() will return nanoseconds instead of milliseconds. Resulting in a wrong delay.

Change it for:

SystemClock.elapsedRealtime() and ensure that the variable different is actually milliseconds and not seconds or anything else.

tomacco
  • 710
  • 4
  • 26
  • Here it is a Java tutorial with all the code you need: https://www.tutorialspoint.com/how-to-schedule-local-notifications-in-android – tomacco Oct 01 '19 at 12:43
  • i add this code in my app but it is showing instant notification. (instead of delay i pass there my millisecond time i.e different) – Shital Oct 01 '19 at 12:57
  • @Shital: Are you using the `AlarmManager`?, are you using a `BroadcastReceiver`?, is the Alarm being scheduled correctly? – tomacco Oct 02 '19 at 07:50
  • @ tomacco : yes , but instead of long futureInMillis = SystemClock. elapsedRealtime () + delay; i use long futureInMillis = different ; (means calculate the time difference between next_action_date (i.e 5/10/19)and current date(i.e 3/10/19) is called different) . but it showing instant notification – Shital Oct 03 '19 at 05:20
  • @Shital I suggest to update the question with the code you have for the AlarmManager, the BroadCast receiver and code that creates and shows the notification. Be as detailed as you can, otherwise people won't be able to help you. – tomacco Oct 03 '19 at 09:38
  • be careful with the line `SystemClock.elapsedRealtimeNanos() ` you are using nano seconds and adding to it a variable called `different` which units are not explicit. is `different` also in nanoseconds?. Be explicit about the units of your variables, one way to do it is to add the unit at the end of the variable name, example (instead of `delay` call it `delayMs` or instead of `distance` call it `distanceMeters`) – tomacco Oct 09 '19 at 07:43
  • @ tomacco : can you please send me , where should i exact change in my code(bz different is my exact time difference between two date) please help... – Shital Oct 10 '19 at 12:44