37

I've been working on push notifications and I am able to implement it and display it on status bar, the problem I am facing is that I want to display it even if the phone is lock, Under the lock screen where it says ("drag to unlock"), I have seen notifications like that but cant find any example to that.

Example: Just like when you received a missed call , it will show it under the lock button on your screen.

Code:

String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
int icon = R.drawable.icon_launcher;
CharSequence tickerText = "MyApplication";
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);
notification.defaults |= Notification.DEFAULT_SOUND|Notification.DEFAULT_VIBRATE|Notification.DEFAULT_LIGHTS;;
CharSequence contentTitle = this.title;
CharSequence contentText = this.message;
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
mNotificationManager.notify(NOTICE_ID, notification);
Emmanuel Devaux
  • 3,327
  • 4
  • 25
  • 30
user606669
  • 1,674
  • 7
  • 27
  • 39
  • 2
    unrelated, you don't necessarily need to save everything as local variables – CrackerJack9 Sep 17 '11 at 00:33
  • 2
    This is not possible right now. – Basic Coder Sep 18 '11 at 20:36
  • @Copa what do you mean? What is your reference? I have several apps which display notifications in the manner the OP describes (notification bar, while screen locked). – CrackerJack9 Sep 18 '11 at 23:03
  • 4
    At this time there is NO possibility to display notifications, widgets or what every onto your lockscreen. If you are using a custom lockscreen this can work - but needn't. A custom lockscreen is a normal app and thats why it works. But the original lockscreen from android itself cant be changed. I have no offical link where Google tells you why this dosent work, but at this time there is no API for doing this job. – Basic Coder Sep 19 '11 at 09:17
  • @Copa: Handcent SMS manages to get a dialog on top of the lock screen. I don't know how though... – Jon Adams Sep 19 '11 at 20:31

5 Answers5

22

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/

Kirit Vaghela
  • 12,572
  • 4
  • 76
  • 80
6

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
Mohamad Bdour
  • 450
  • 5
  • 13
5

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

Community
  • 1
  • 1
VirtualProdigy
  • 1,637
  • 1
  • 20
  • 40
4

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)

Alex Misiulia
  • 1,542
  • 17
  • 16
2

The notifications you have seen may actually be widgets placed on a custom widget host lockscreen.

If you look at the android platform source code for InstallWidgetReceiver as late as 4.4.3 here:

https://android.googlesource.com/platform/packages/apps/Launcher3/+/master/src/com/android/launcher3/InstallWidgetReceiver.java

You will see this note by the author:

/** * We will likely flesh this out later, to handle allow external apps to place widgets, but for now, * we just want to expose the action around for checking elsewhere. */

And you can see that InstallWidgetReceiver.java is in fact not fleshed out by google in the same way as InstallShortCutReceiver.java is. So it seems at least up to 4.4.3 you cant add widgets to the native lock screen in the same way that you can for example add a shortcut to the homescreen using InstallShortCutReceiver.

Unless you build your own lockscreen app as a widget host and the user installs in lieu of the native you may be out of luck using a widget.

Another approach however is to just us an activity that sets getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);

This will display your activity whether the screen is locked or not. Dismissing this activity when the screen is locked will display the locked screen.

Sani Elfishawy
  • 663
  • 1
  • 7
  • 14