NotificationCompat.Builder on api 15 fail to notify time

515 views Asked by At

I trying to use Notification on API 15 with NotificationCompat.Builder, but if I pass 10 seconds( 100000 in milliseconds) as when parameters the notification starts immediately. Where is the problem? I want to start the notification after 10 seconds, how can I do it?

this is the code of my method

 private void createNotification(long when,String data){
        String notificationContent ="Notification Content Click Here to go more details";
        String notificationTitle ="This is Notification";
        Bitmap largeIcon = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
        int smalIcon =R.drawable.ic_launcher;
        String notificationData="This is data : "+data;
        Intent intent =new Intent(getApplicationContext(), NotificationDetailsActivity.class);
        intent.putExtra("textkey", notificationData);
        intent.setData(Uri.parse("content://" + when));

        PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, PendingIntent.FLAG_NO_CREATE);

        NotificationManager notificationManager =(NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);

        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(
                getApplicationContext())
                .setWhen(when)
                .setContentText(notificationContent)
                .setContentTitle(notificationTitle)
                .setSmallIcon(smalIcon)
                .setAutoCancel(true)
                .setTicker(notificationTitle)
                .setLargeIcon(largeIcon)
                .setDefaults(Notification.DEFAULT_LIGHTS| Notification.DEFAULT_VIBRATE| Notification.DEFAULT_SOUND)
                .setContentIntent(pendingIntent);

        Notification notification=notificationBuilder.build();
        notificationManager.notify((int) when, notification);
    }

1

There are 1 answers

3
ianhanniballake On BEST ANSWER

That isn't what setWhen() does. Per the documentation:

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

This has to do with when the event that triggered the notification happened (i.e., you receive a message, a social network update happens, etc) in Unix time, hence why the default is what is returned by System.currentTimeMillis() (i.e., right now). Passing in 100,000 (which is in fact 100 seconds) would mean January 1st, 1970, 12:01:40am UTC.

If you want to schedule a notification to appear at some future time, you should set an alarm via AlarmManager which allows you to trigger your notification code at the specified time.