Android: Adding notification to status bar - android

I have created a notification with NotificationCompat.Builder but i don't know how to show it in status bar. I tried using
NotificationManager notificationManager = (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(mId, mBuilder.build());
but it doesn't work on 2.2 so i need another way...
this is my notification
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notification)
.setContentTitle("Message")
.setContentText("Notification");

but it doesn't work on 2.2
Yes it does.
This sample application shows an IntentService downloading a file and displaying a Notification using NotificationCompat.Builder. It uses code pretty much like yours for actually raising the Notification:
NotificationManager mgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
mgr.notify(NOTIFY_ID, b.build());
This code works fine on Android 2.2, as I just tried it (again) on an emulator, and the Notification displays as expected.

Related

how to remove a persistent notification after setongoing(true)?

Here is the code that I'm using to create a notification in Android. This notification is shown as intended and isn't getting removed by swiping the notification.
But I'm unable to remove it programmatically. How should I remove it?
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(getApplicationContext(), Constants.CHANNEL_ID)
.setSmallIcon(R.drawable.small)
.setContentTitle("persistent")
.setContentText("cant remove").setOngoing(true);
mBuilder.build();
In this answer there's a nice explanation on how to do it:
https://stackoverflow.com/a/19268653/3853450
In your case should be something like this:
NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(Constants.CHANNEL_ID);
Also, as given in the original answer:
NotificationManager

BigPictureStyle Notification not showing properly in Xiaomi MIUI-8

I wanted to display a bigPictureStyle Notification in MiUi(don't want to use custom view) using the following code--
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(title)
.setContentText(msg)
.setStyle(new NotificationCompat.BigPictureStyle()
.bigPicture(BitmapFactory.decodeResource(getResources(), R.drawable.b))
.setBigContentTitle(title));
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(10, mBuilder.build());
It works perfectly on raw android but in Xiaomi MIUI 8 (Android Lollipop based) it shows a shadow at the bottom of expanded notification view as in the picture--
How to remove this bottom shadow?
The only solution for now is using a custom view with similar looking big content view.

Notification Not Appearing On Watch

I'm creating the following notification in my app:
int notificationId = 001;
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.icon)
.setContentTitle(StationUtils.getStationModeString("My Notification")
.setContentText("Good Notification");
NotificationManager mNotifyMgr =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
mNotifyMgr.notify(notificationId, notificationBuilder.build());
NotificationManagerCompat notificationManager =
NotificationManagerCompat.from(MainActivity.this);
notificationManager.notify(notificationId, notificationBuilder.build());
It appears on my phone, but it doesn't appear on my watch connected to my phone. Is there something missing from the code that's required for Android Wear? Does it have to be a release signed build? I checked settings and the watch does have access to notifications.
Thanks
If you are using Android 5.0 be sure that interuption settings are not set to "None".
Silence your device with "Do not disturb"

regular notification wont show X close button in android

I created a notification using android.support.v4.app.NotificationCompat.Builder. The notification shows up fine in the notification area.But it doesn't show the default close button on android 4.2.
http://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html#setOngoing(boolean)
As per the docuentation,its a type of regular notification which should show the X close button but it doesn't.
Using the following code:
mBuilder = new NotificationCompat.Builder(context);
mBuilder.setSmallIcon(iconId);
mBuilder.setContentTitle(titleText);
mBuilder.setContentText(moreinfoText);
mBuilder.setOngoing(false); //to make it regular
PendingIntent pintent =
PendingIntent.getActivity(context,noteId,resultIntent,PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(pintent);
NotificationManager mNotificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(noteId, mBuilder.build());
Searched on Google but couldn't find anything similar.
Please suggest.
Thanks for your time!

How to show notification in status bar?

So i have created this notification in my activity
Notification n = new Notification.Builder(getApplicationContext())
.setContentTitle("New mail from " + sender)
.setContentText(subject)
.setSmallIcon(R.drawable.notification)
.build();
How can i now show it in status/notification bar along with the sound?
There is some good documentation at android developer site
and have a look at the NotificationManager doc's
Here you go, some code...:
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
mNotificationManager.notify(mId, n);
Note that it is also a goo idea to add your intend to the notification...
mBuilder.setContentIntent(resultPendingIntent);
To add sound you can use the Notification.Builder.setSound() method.
How can i now show it in status/notification bar along with the sound?
Use the Notification.Builder.setSound() method.
You can get the default ringtone like this:
Uri uri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
and then set it as notification sound:
Notification.Builder(getApplicationContext()).setSound(uri);
and then after you've built your notification you launch it with:
myNotificationManager.notify(myNotificationId, n);

Categories

Resources