I'm using Android Oreo's new notification system. I want to schedule a notification in the future. I made a test program that is supposed to fire a notification 5 seconds later. Instead, it displays the notification immediately. Here is my code:
public class MainActivity extends AppCompatActivity {
private NotificationManager manager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
NotificationChannel notificationChannel = new NotificationChannel("default",
"primary", NotificationManager.IMPORTANCE_DEFAULT);
manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.createNotificationChannel(notificationChannel);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Notification notification = new Notification.Builder(getApplicationContext(), "default")
.setContentTitle("title")
.setContentText("body")
.setWhen(Calendar.getInstance().getTimeInMillis() + 5000)
.setSmallIcon(android.R.drawable.stat_notify_chat)
.setAutoCancel(true)
.build();
manager.notify(123, notification);
}
});
}
}
Thanks!