My application could play some audio.
Once user press at the audio title the notification with the play/pause/rewind buttons appears at the notification drawer.
I noticed that in Android O (8) this notification appears with "heads-up" effect.
I don't need heads-up notification, I need just silent common notification. How can I disable "heads-up" effect?
NotificationCompat.Builder notificationBuilder;
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
notificationBuilder = new NotificationCompat.Builder(getApplicationContext(), Constants.CHANNEL_ID);
} else {
notificationBuilder = new NotificationCompat.Builder(getApplicationContext());
}
Notification notification = notificationBuilder
.setSmallIcon(R.drawable.tray_icon)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
.setWhen(System.currentTimeMillis())
.setContent(remoteViews)
.setChannelId(Constants.CHANNEL_ID)
.build();
notification.defaults = 0;
Related
I'm trying to show a bubble notification from a phone state receiver after call ends and its displaying as an ordinary notification. But it displays a bubble notification when I send notification from a button click event.
Here is manifest file bubble activity
<activity android:name=".BubbleActivity"
android:label="#string/to_do_bubble"
android:allowEmbedded="true"
android:documentLaunchMode="always"
android:resizeableActivity="true">
Snippet for showing bubble notification in phone state broadcast receiver
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = "nameHere";
String description = "DescriptionHere";
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel channel = new NotificationChannel("1001", name, importance);
channel.setDescription(description);
channel.setAllowBubbles(true);
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.createNotificationChannel(channel);
}
// Create bubble intent
Intent target = new Intent(context, PopUpBubbleActivity.class);
PendingIntent bubbleIntent =
PendingIntent.getActivity(context, 0, target, 0 /* flags */);
// Create bubble metadata
Notification.BubbleMetadata bubbleData =
new Notification.BubbleMetadata.Builder()
.setDesiredHeight(600)
.setIcon(Icon.createWithResource(context, R.mipmap.ic_launcher))
.setIntent(bubbleIntent)
.setAutoExpandBubble(true)
.setSuppressNotification(true)
.build();
Person chatBot = new Person.Builder()
.setBot(true)
.setName("BubbleBot")
.setImportant(true)
.build();
Notification.Builder builder =
new Notification.Builder(context, "1001")
.setContentTitle("New message")
.setContentText("Click to open new message")
.setContentIntent(bubbleIntent)
.setSmallIcon(R.mipmap.ic_launcher)
.setBubbleMetadata(bubbleData)
.setAutoCancel(true)
.addPerson(chatBot);
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
notificationManager.notify(1001, builder.build());
}
}
This code is displaying ordinary notification instead of bubble notification after call end.
This same code(with certain changes required to run from activity) I tried on button click and it's displaying a bubble notification as expected.
I'm running on emulator with API level 29 and compileSDKVersion 29 and targetSDKVersion 29
This question already has answers here:
non removable notification
(4 answers)
Closed 4 years ago.
I am working on music application so i need to display notification on notification bar. for that i am using custom notification. The following is the code for notification for Android Oreo
String NOTIFICATION_CHANNEL_ID = "001";
RemoteViews notificationLayout = new RemoteViews(getPackageName(), R.layout.mynotification);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "My Notifications", NotificationManager.IMPORTANCE_LOW);
notificationChannel.setDescription("Channel description");
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.createNotificationChannel(notificationChannel);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(MainActivity.this, NOTIFICATION_CHANNEL_ID);
notificationBuilder.setSmallIcon(R.drawable.exo_edit_mode_logo)
.setCustomContentView(notificationLayout)
.setAutoCancel(true)
.setPriority(Notification.PRIORITY_DEFAULT);
NotificationManagerCompat com = NotificationManagerCompat.from(this);
com.notify(001, notificationBuilder.build());
}
This code is working fine, but when swipe the notification from notification bar it will be removed. But my requirement is not remove when swipe the notification.
I tried with setAutoCancel(true) and setAutoCancel(false). But not working it will be removed. So how to stick the notification here.
Please guide me how to do this.
Thanks In Advance
use setOngoing(true) to achieve this
You have to add this setOngoing(true)
Try:
Notification.Builder builder = new Notification.Builder(this, CHANNEL_ID)
.setContentTitle(getString(R.string.app_name))
.setContentText("Text")
.setSmallIcon(R.mipmap.ic_launcher)
.setAutoCancel(false)
.setOngoing(true);
Notification notification = builder.build();
I create a Notification using NotificationCompat.Builder. That notification gets an action button:
notification = notificationBuilder
.setContentTitle(getString(R.string.blabla)).
.setContentText("")
.setSmallIcon(R.drawable.blabla)
.setContentIntent(pendingIntentOpenApp)
.addAction(android.R.drawable.ic_menu_close_clear_cancel, "Cancel", pendingIntentAction)
.build();
The notification displays, but the action is repeated three times:
I cannot figure out what I am doing wrong, so I appreciate any help.
You can create notification by using following code:
var mBuilder = NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle("Title")
.setContentText("Description")
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.addAction(R.drawable.ic_notification,"OK",pendingIntent)
.setChannelId(chanelId).build()
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val mChanel = NotificationChannel(id, name, NotificationManager.IMPORTANCE_HIGH)
notificationManager.createNotificationChannel(mChanel)
notificationManager.notify(channelId, mBuilder)
}else{
notificationManager.notify(channelId, mBuilder)
}
This might be duplicate with this question
but still not able to implement notification without heads-up view.
Problem statement : want to show the notification when playing a song. binding one notification with music service that floats on top of the window (Shows a heads-up view) it's very annoying.
Is there any way to disable that heads-up view, just want to show a small icon in status bar.
mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(getApplicationContext(), CHANNEL)
.setContentTitle("titile")
.setContentText("desc")
.setSmallIcon(R.drawable.logo)
.setColor(getResources().getColor(R.color.colorPrimary))
.setPriority(NotificationCompat.PRIORITY_LOW)
.setAutoCancel(true);
Notification notification = notificationBuilder.build();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel(
CHANNEL, "Halt", NotificationManager.IMPORTANCE_NONE
);
mNotificationManager.createNotificationChannel(notificationChannel);
}
mNotificationManager.notify(NOTIFICATION_ID, notification);
After spending some time, I just uninstall and reinstall the application and start works.
Buggy Rocks
I have a simple method which onclick() of a Button should generate status bar notification, but for some reason, it isn't showing up.
public void createNotification() {
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(android.R.drawable.ic_dialog_alert)
.setContentTitle("Notification!")
.setContentText("This is my first notification!");
Notification notification = builder.build();
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(0, notification);
}
In Android 8 (API level 26), all notifications must be assigned to a channel.
This work for me:
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getContext(), CHANNEL_ID)
.setSmallIcon(R.drawable.emo_no_paint_120)
.setContentTitle("title")
.setContentText("content")
.setColor(Color.parseColor("#009add"))
.setAutoCancel(true);
NotificationManager notificationManager = (NotificationManager) getContext().getSystemService(
NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
notificationManager.createNotificationChannel(mChannel);
}
notificationManager.notify(0, mBuilder.build());
And you should add AppCompat library
implementation 'com.android.support:support-compat:27.1.0'
You could check official Android Docs
Your code is okay. But since you are targeting Android 8.0, you need to implement Notification Channels since this the preferred way in Android Oreo.
https://developer.android.com/guide/topics/ui/notifiers/notifications.html