Android how to show notification on screen

Create Notification using NotificationCompat.Builder but make sure to put visibility to public like

NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
        builder
        .setContentTitle("Title")
        .setContentText("content")
        .setSmallIcon(R.mipmap.ic_launcher)
        .setVisibility(NotificationCompat.VISIBILITY_PUBLIC);//to show content in lock screen

I fixed this by adding this line to notification builder

builder.setOngoing(true);

It will also make notification not cancelable by user, but it solves the problem.

Credits to: Marian Klühspies (link)


Have you tried creating the alertdialog with a flag? The flag_show_when_locked should do the trick. Please refer to this thread, you should find a more detailed answer here. Android Lock Screen Widget


Create Notification using NotificationCompat.Builder

NotificationCompat.Builder mBuilder =   new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_launcher) // notification icon
            .setContentTitle("Notification!") // title for notification
            .setContentText("Hello word") // message for notification
            .setAutoCancel(true); // clear notification after click
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pi = PendingIntent.getActivity(this,0,intent,Intent.FLAG_ACTIVITY_NEW_TASK);
mBuilder.setContentIntent(pi);
NotificationManager mNotificationManager =
                    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, mBuilder.build());

Push Notification on locked Screen http://www.hongkiat.com/blog/android-lock-screen-notifications/