Notification without heads-up View - android

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

Related

Android 11 - Foreground service notification isn't visible

I have an android app that makes use of Foreground Services. My foreground service is supposed to display a notification.
I have updated one of my devices to Android 11 and the foreground service's notification is not being displayed. The foreground notification works as expected and is visible on all previous versions.
My code for starting a foreground service with a notification is as follows:
String channelName = "MyChannel";
NotificationChannel chan = new NotificationChannel("com.my.package", channelName, NotificationManager.IMPORTANCE_NONE);
chan.setLightColor(Color.BLUE);
chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.createNotificationChannel(chan);
RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.my_layout);
remoteViews.setImageViewResource(R.id.my_notification_id, R.drawable.my_icon);
remoteViews.setTextViewText(R.id.my_title, "Title");
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context, "com.my.package");
notificationBuilder.setOngoing(true)
.setSmallIcon(R.drawable.ic_image_icon)
.setContentTitle("My Title")
.setPriority(NotificationManager.IMPORTANCE_MIN)
.setCategory(Notification.CATEGORY_SERVICE)
.setStyle(new android.support.v4.media.app.NotificationCompat.DecoratedMediaCustomViewStyle()
.setMediaSession(new MediaSessionCompat(context, MY_TAG).getSessionToken()))
.setColor(0x169AB9)
.setWhen(System.currentTimeMillis())
.setOnlyAlertOnce(true)
.setColorized(true);
PendingIntent contentIntent = PendingIntent.getActivity(context, MYMConstants.NOTIFICATION_GO_TO_ACTIVITY_REQUEST_CODE,
new Intent(context, MainActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);
notificationBuilder.setContent(remoteViews);
notificationBuilder.setContentIntent(contentIntent);
Notification notification = notificationBuilder.build();
startForeground(10000, notification);
The foreground service is started as follows:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(serviceIntent);
} else {
context.startService(serviceIntent);
}
I searched for the official documentation on Android 11 and underlying changes to Foreground services, but I couldn't find anything. Could someone help me out with this?
Nothing has changed the way how notifications are added to Foreground Service as part of Android 11.
I did find where the problem lies though. This piece of code above was causing the problem.
.setStyle(new android.support.v4.media.app.NotificationCompat.DecoratedMediaCustomViewStyle()
.setMediaSession(new MediaSessionCompat(context, MY_TAG).getSessionToken()))
Removing this fixed the issue.
To utilize setStyle(), we need to replace android.support.v4.media.app by androidx.media.app.

Notification is removed from status bar when I close the app

I send a simple notification to the user when they open the app. When I swipe the notification away from the status bar, the notification is also deleted.
I just want the notification to stay on the status bar even if I close the app like whats app Instagram and some other apps.
This is how I send notification
Context context = getActivity();
Intent intent1 = new Intent(context,MainActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(context, 0, intent1, 0);
Notification.Builder builder = new Notification.Builder(context)
.setTicker("Notification")
.setContentTitle("Important Message")
.setContentText("This is an example of a push notification using a Navigation Manager")
.setSmallIcon(R.drawable.ic_add)
.setContentIntent(pIntent);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
//These are necessary for the notification to pop up
if(Build.VERSION.SDK_INT < Build.VERSION_CODES.O){
builder.setPriority(Notification.PRIORITY_MAX);
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
builder.setSound(alarmSound);
builder.setLights(Color.BLUE, 500, 500);
}
//after android O we must use notification channels
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
{
String channelId = "Your_channel_id";
NotificationChannel channel = new NotificationChannel(
channelId,
"Reminder to remind to review your notes",
NotificationManager.IMPORTANCE_HIGH);
channel.setDescription("Hello Dear friends"); //this is to test what this is
notificationManager.createNotificationChannel(channel);
builder.setChannelId(channelId);
}
Notification notification = builder.build();
notification.flags = Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, notification);
}
P.S1: I don't want the notification to persist like in foreground services. I just want to not go away when the app is swiped away, but the user can get rid of the notification anytime they want.
P.S2: I am not using any services, just a simple notification. Like e.g.letting the user know that they collected some badges today.

NotificationManager.IMPORTANCE_HIGH still no sound

I am trying to create notifications in android but no matter what importance and priority I set, there's no sound. I am assuming sound (ringtone) is handle by android itself I don't need to provide any mp3/wav file. I am trying on android 8.1 (actual device), 8.0 (emulator) and 8.1 (emulator). Notification channel created on actual device has sound off by default, I don't know why and on emulator sound is on but still no sound played on notification
Here is my code:
public void sendMessage(View view) {
createNotificationChannel();
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setContentTitle("New Notification")
.setContentText("Lorem Ipsum")
.setPriority(NotificationCompat.PRIORITY_HIGH);
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(1, builder.build());
}
private void createNotificationChannel() {
// Create the NotificationChannel, but only on API 26+ because
// the NotificationChannel class is new and not in the support library
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = "basic-channel";
String description = "Lorem Ipsum";
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
channel.setDescription(description);
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
}
Channel on Actual Device
Channel on Emulator
Root Cause:
For actual device, that's OEM's problem, which in my case is Xiaomi and I found this link threema.ch/en/faq/notification_channels_xiaomi which says that xiaomi sets sound=off for all app except select few like FB, whatsApp, etc.
For emulator, we need to complete the setup process after which notification starts making sound.
When you build the notification, you need to indicate that you want to use the default system values:
builder.setDefaults(Notification.DEFAULT_ALL)
And remember to clear the app data or reinstall the app to properly recreate the notification channel. A notification channel will retain its initial configuration even if you recreate it in code.
Check this out :
//Define sound URI
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setContentTitle("New Notification")
.setContentText("Lorem Ipsum")
.setPriority(NotificationCompat.PRIORITY_HIGH);
.setSound(soundUri); //This sets the sound to play

Disable swipe removal notification in Android Oreo [duplicate]

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();

Android O: disable heads-up

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;

Categories

Resources