Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android how to show notification on screen

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); 
like image 948
user606669 Avatar asked Sep 16 '11 09:09

user606669


People also ask

How do I make notifications visible on Android?

If you don't find the relevant settings in the app, you can turn on Android notifications for specific apps under Settings > Apps & Notifications > [App name] > Notifications.

Why are my notifications not showing on screen?

Cause of Notifications Not Showing up on AndroidDo Not Disturb or Airplane Mode is on. Either system or app notifications are disabled. Power or data settings are preventing apps from retrieving notification alerts. Outdated apps or OS software can cause apps to freeze or crash and not deliver notifications.

Why are my notifications not showing up on my Lock screen Android?

Enable apps to push notifications and display them on the lock screen: Open Settings, search for and access Apps, locate and access the app in question, and touch Notifications. Enable Allow notifications and set Lock screen notifications to Show.


2 Answers

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/

like image 165
Kirit Vaghela Avatar answered Sep 18 '22 06:09

Kirit Vaghela


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 
like image 35
Mohamad Bdour Avatar answered Sep 21 '22 06:09

Mohamad Bdour