Although @cja's answer may be right, although it lacks a few lines of code (a notification does not appear or will not appear in your notification tray).
This is the full working function:
public void createNotification() { NotificationCompat.Builder notification = new NotificationCompat.Builder(this); notification.setTicker( "Ticker Text" ); notification.setSmallIcon(R.drawable.ic_launcher); notification.setContentTitle( "Content Title" ); notification.setContentText( "Content Text" ); notification.setAutoCancel( false ); notification.setOngoing( true ); notification.setNumber( ++NotificationCount ); Intent intent = new Intent(this, MainActivity.class); PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0); notification.setContentIntent(pIntent); notification.build(); NotificationManager nManger = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); nManger.notify(NotificationID, notification.build()); }
NotificationID int is your notification identifier.
you can clean it using this:
public void clear() { NotificationManager oldNoti = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); oldNoti.cancel( NotificationID ); }
make sure for notification.setAutoCancel( false ); set to false , so it will not be cleared when you click the clear button or if there are transfer gestures.
multiple lines of code are initially sent from the @cja message.
greetings / happy encodings ...
source share