3

I have an app which receives push notifications from custom webserver using the new gcm api. Everything works fine. However, I want to display the notifications on the lock screen too.

I googled around and found this post, but it won't help: Android how to show notification on screen

Is there any simple way to achieve this? Thanks for your help!

EDIT //

Here is some code.

In the GCMHandler I use this snippet to show the notification:

   private void sendNotification() {
    mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);

    Intent intent = new Intent(this, MainActivity.class);
    intent.putExtra("message", message);
    intent.putExtra("pbid", pbid);
    intent.putExtra("alarm", alarm);

    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this)
                    .setSmallIcon(R.drawable.ic_launcher)
                    .setContentTitle(alarm ? getString(R.string.txt_alarm_message_title) : getString(R.string.txt_info_message_title))
                    .setContentText(message);

    // play alarm tone
    if (alarm) playRingtone();

    mBuilder.setContentIntent(contentIntent);
    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}

And in the application's main activity I used the code from the post above:

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    setContentView(R.layout.act_main);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON, WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED, WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
...
}

The problem is, that the notification is shown, but not available on the lock screen. If the screen is screen is locked, no push notification will be received ;(

Community
  • 1
  • 1
Robin
  • 709
  • 2
  • 8
  • 20
  • what is the problem when you tried the link. Provide some code which you tried using flag – Sree Jul 02 '14 at 10:04
  • I posted the code in the edit of my original post. – Robin Jul 02 '14 at 10:14
  • possible duplicate of [BroadcastReceiver not called when screen locked in Android](http://stackoverflow.com/questions/18481641/broadcastreceiver-not-called-when-screen-locked-in-android) – Eran Jul 02 '14 at 14:51
  • Thanks. I tried it out but it still doesn't work on Android 4.4.2 ;-( Do you have any other suggestions? – Robin Jul 04 '14 at 12:05
  • @Robin : Hello, have you find the answer for this question? – Huy Tower Aug 24 '15 at 03:59

1 Answers1

-2

try this it will work it.

int currentapiVersion = android.os.Build.VERSION.SDK_INT;

    if (currentapiVersion >= android.os.Build.VERSION_CODES.LOLLIPOP) {
        Notification noti = new Notification.Builder(
                getApplicationContext())
                .setContentTitle("New mail from " + " ")
                .setContentText(msg).setContentIntent(contentIntent)
                .setDefaults(1).setSmallIcon(R.drawable.gcm_cloud).build();
        PowerManager pm = (PowerManager) this
                .getSystemService(Context.POWER_SERVICE);

        WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK
                | PowerManager.ACQUIRE_CAUSES_WAKEUP, "TAG");
        wl.acquire(15000);

        mNotificationManager.notify(NOTIFICATION_ID, noti);

    } 
RDY
  • 613
  • 1
  • 9
  • 25
  • This will only wake up the phone. The OP is asking how to show the notification when the screen is locked. – T-D Jul 14 '15 at 22:53
  • its display for lock screen notification just try this. then comment. – RDY Jul 15 '15 at 06:19
  • This answer is wrong. Question is how to show notification on Lock screen page, not in notification bar system. – Huy Tower Aug 24 '15 at 03:57