Android Show Notification with a Popup on Top of Any Application

android show notification with a popup on top of any application

If you want use Heads-up Notifications like this:

Sample Image

You must change Notification priority or NotificationChannel importance.

The notification priority, set by setPriority(). The priority determines how intrusive the notification should be on Android 7.1 and lower. (For Android 8.0 and higher, you must instead set the channel importance)

On Android 7.1 (API level 25) and lower:

  • Set notification priority to NotificationCompat.PRIORITY_HIGH or NotificationCompat.PRIORITY_MAX.
  • Set ringtone and vibrations - you can use setDefaults(Notification.DEFAULT_ALL)

Android 8.0 (API level 26) and higher:

  • Set notification channel priority to NotificationManager.IMPORTANCE_HIGH

Notification:

NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setSmallIcon(R.drawable.ic_wald_poi)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_poi))
.setColor(getResources().getColor(R.color.primary))
.setContentTitle(getString(R.string.hello))
.setContentIntent(notificationPendingIntent)
.setContentText(String.format(getString(R.string.notification), viewObject.getTitle()))
.setDefaults(NotificationCompat.DEFAULT_ALL)
.setStyle(bigText)
.setPriority(NotificationCompat.PRIORITY_HIGH) // or NotificationCompat.PRIORITY_MAX

Notification channel:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// Create the NotificationChannel
val name = getString(R.string.notification_channel_name)
val descriptionText = getString(R.string.notification_channel_description)
val importance = NotificationManager.IMPORTANCE_HIGH
val mChannel = NotificationChannel(NOTIFICATION_CHANNEL_ID, name, importance)
mChannel.description = descriptionText
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannel(mChannel)
}

Important

If you'd like to further customize your channel's default notification behaviors, you can call methods such as enableLights(), setLightColor(), and setVibrationPattern() on the NotificationChannel. But remember that once you create the channel, you cannot change these settings and the user has final control of whether these behaviors are active. Other option is to uninstall and install application again.
Read more


Examples of conditions that may trigger heads-up notifications include:

  • The user's activity is in fullscreen mode (the app uses
    fullScreenIntent).
  • The notification has high priority and uses
    ringtones or vibrations on devices running Android 7.1 (API level 25)
    and lower.
  • The notification channel has high importance on devices
    running Android 8.0 (API level 26) and higher.

Priority:

Notification.PRIORITY_HIGH and Notification.PRIORITY_MAX was deprecated in API level 26. use NotificationCompat instead.

Here is more info :-)

How do I make an Android local notification that doesn't pop-up, just shows up on the notification list?

You need to use a low priority for channel you created

NotificationManager.IMPORTANCE_LOW  or NotificationManager.IMPORTANCE_MIN 

From Android Oreo, you need to use notification channels for creating Notifications.

For Example : When you create a channel, with id "Alerts" ,
you can define its properties like, its importance , sound etc...

The various Importance Attributes used are as follows:

IMPORTANCE_DEFAULT:
Default notification importance: shows everywhere, makes noise, but does not visually intrude.
Constant Value: 3

IMPORTANCE_HIGH :
Higher notification importance: shows everywhere, makes noise and peeks.
May use full screen intents.

[This will keep showing over other apps and again like whatsapp message notification]
This works so because of its importance.
Constant Value: 4

IMPORTANCE_LOW
Low notification importance: shows everywhere, but is not intrusive.
Constant Value: 2

IMPORTANCE_MIN
Min notification importance: only shows in the shade, below the fold.
Constant Value: 1

So once you define a channels properties it could be changed by only the user through notification intent.
Which i mentioned here : https://stackoverflow.com/a/54199316/7039593

Using a low priority / min min importance may solve your problem

How to show notifiction top screen in android?

I found a solution for you. Hope it may help you.

//build notification
NotificationCompat.Builder builder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("Ping Notification")
.setContentText("Tomorrow will be your birthday.")
.setDefaults(Notification.DEFAULT_ALL) // must requires VIBRATE permission
.setPriority(NotificationCompat.PRIORITY_HIGH) //must give priority to High, Max which will considered as heads-up notification
.addAction(R.drawable.dismiss, getString(R.string.dismiss), piDismiss)
.addAction(R.drawable.snooze, getString(R.string.snooze), piSnooze);

//set intents and pending intents to call service on click of "dismiss" action button of notification
Intent dismissIntent = new Intent(this, MyService.class);
dismissIntent.setAction(ACTION_DISMISS);
PendingIntent piDismiss = PendingIntent.getService(this, 0, dismissIntent, 0);

//set intents and pending intents to call service on click of "snooze" action button of notification
Intent snoozeIntent = new Intent(this, MyService.class);
snoozeIntent.setAction(ACTION_SNOOZE);
PendingIntent piSnooze = PendingIntent.getService(this, 0, snoozeIntent, 0);

// Gets an instance of the NotificationManager service
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
/* Notification for oreo*/
String channelId = "channel-01";
String channelName = "Demo";
int importance = NotificationManager.IMPORTANCE_HIGH;

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationChannel mChannel = new NotificationChannel(
channelId, channelName, importance);
notificationManager.createNotificationChannel(mChannel);
}
//to post your notification to the notification bar with a id. If a notification with same id already exists, it will get replaced with updated information.
notificationManager.notify(0, builder.build());

Minimum SDK is Lolipop.

how to show popup notification when android device screen off

  /Just implement one line/

.setDefaults(Notification.DEFAULT_VIBRATE | Notification.DEFAULT_LIGHTS | Notification.DEFAULT_SOUND)

/******************************/

notification = mBuilder.setSmallIcon(R.mipmap.ic_launcher)
.setTicker(title)
.setShowWhen(true)
.setAutoCancel(true)
.setContentTitle(title)
.setContentIntent(resultPendingIntent)
.setSound(alarmSound)
.setStyle(inboxStyle)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.mipmap.ic_launcher)
.setDefaults(Notification.DEFAULT_VIBRATE | Notification.DEFAULT_LIGHTS | Notification.DEFAULT_SOUND).setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(), R.mipmap.ic_launcher))
.setContentText(message)
.build();

/**Implement below Method***/

/**And call for "wakeUpLock" Method inside onMessageReceived()**/

private void wakeUpLock()
{

PowerManager pm = (PowerManager)MyGcmListenerService.this.getSystemService(Context.POWER_SERVICE);

boolean isScreenOn = pm.isScreenOn();

Log.i(TAG, "screen on: "+ isScreenOn);

if(isScreenOn==false)
{
Log.i(TAG, "screen on if: "+ isScreenOn);

PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK |PowerManager.ACQUIRE_CAUSES_WAKEUP |PowerManager.ON_AFTER_RELEASE,"MyLock");

wl.acquire(10000);

PowerManager.WakeLock wl_cpu = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,"MyCpuLock");

wl_cpu.acquire(10000);
}

}

Set Firebase Notification to appear as Popup like whatsapp in Android studio

You should use heads-up notifications

For this purposes you should use notification channel with high a importance: How to set importance in the notification channel.

In this answer you can find code sample, how to show notification in the channel with high importance(to show notification even if the app in the foreground):
https://stackoverflow.com/a/67953864/16210149

Hope it would be helpful.



Related Topics



Leave a reply



Submit