Notification icon is not changed with .setSmallIcon - java

I am implementing Notifications in android. I want to change the icon of notification, but the problem is that setSmallIcon is not working. My custom icon is showing in the lollipop device but not showing the custom icon above lollipop devices.
I have tried the following code:
send_notification.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Bitmap icon = BitmapFactory.decodeResource(getResources(),R.drawable.book2);
NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this)
.setContentTitle("New Book Added")
.setContentText("Android with java")
.setStyle(new NotificationCompat.BigPictureStyle()
.bigPicture(icon)
.bigLargeIcon(null));
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
builder.setSmallIcon(R.drawable.ic_notification);
} else {
builder.setSmallIcon(R.drawable.notification);
}
NotificationManager manager= (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.notify(1,builder.build());
}
}
The big picture is also not showing. Please Help.
EDIT:
Bitmap icon = BitmapFactory.decodeResource(getResources(), R.drawable.test);
String channelId = getString(R.string.default_notification_channel_id);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, channelId);
if (remoteMessage.getData().size() > 0) {
String title = remoteMessage.getData().get("title");
String text = remoteMessage.getData().get("body");
builder.setContentTitle(title)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentText(text)
.setLargeIcon(icon)
.setPriority(Notification.PRIORITY_MAX)
.setStyle(new NotificationCompat.BigPictureStyle()
.bigLargeIcon(icon));
} else if (remoteMessage.getNotification() != null) {
String title = remoteMessage.getNotification().getTitle();
String text = remoteMessage.getNotification().getBody();
builder.setContentTitle(title)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentText(text)
.setLargeIcon(icon)
.setPriority(Notification.PRIORITY_MAX)
.setStyle(new NotificationCompat.BigPictureStyle()
.bigLargeIcon(icon));
}
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel(channelId, "Testing channel", NotificationManager.IMPORTANCE_DEFAULT);
manager.createNotificationChannel(notificationChannel);
}
manager.notify(1, builder.build());

Try uninstalling and reinstalling the 100% fresh App. Android keeps cache so things work more quickly and the notification is one of those things.
Uninstalling and reinstalling worked for my issue some time ago.
Also do remember that in Android Oreo and above, in order for notifications to work; notification channels must be created and also the notification that you're creating should be assigned to one of them.
EDIT
What I've tried (and it works for me): (Sample in Kotlin but should be similar enough);
fun onClick(view: View) {
var icon_resource = R.drawable.ic_mtrl_chip_close_circle
if(Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {
icon_resource = R.drawable.ic_mtrl_chip_checked_circle
}
val icon = BitmapFactory.decodeResource(resources,R.mipmap.ic_launcher_round)
val notification = NotificationCompat.Builder(this)
.setSmallIcon(icon_resource)
.setContentTitle("New Book Added")
.setContentText("Android with java")
.setLargeIcon(icon)
.setStyle(NotificationCompat.BigPictureStyle()
.bigPicture(icon)
.bigLargeIcon(null)).build()
val manager= getSystemService(NOTIFICATION_SERVICE) as NotificationManager
manager.notify(1,notification)
}
SECOND EDIT
I think there's a problem with your image, (as there was in mine; sizes or something). After spending a lot of time trying to figure out why it didn't display the launcher icon in the notifications I decided to try with one of my images that I had laying around...
Tested with Android O; API 28
And here are the results:
Feel free to try with the original image I used:
Here's the code that is powering these results; it's not very different from the first one...
// Parametherise this drawable with an if statement of your own
var icon_resource = R.drawable.ic_mtrl_chip_close_circle
val manager= NotificationManagerCompat.from(this)
// This is required because the testing device is an Android O
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
manager.createNotificationChannel(
NotificationChannel("CUSTOM_NOTIFICATIONS", "StackOverflow", NotificationManager.IMPORTANCE_HIGH)
)
}
val icon = BitmapFactory.decodeResource(resources,R.mipmap.dafuq)
val notification = NotificationCompat.Builder(this, "CUSTOM_NOTIFICATIONS")
.setSmallIcon(icon_resource)
.setContentTitle("New Book Added")
.setContentText("Android with java")
.setLargeIcon(icon)
.setStyle(NotificationCompat.BigPictureStyle()
.bigPicture(icon)
.bigLargeIcon(null)).build()
manager.notify(1,notification)
Additional info:
When I'm using a wrong image, I get this pesky log in my Logcat. Check for the same or similars!
2019-04-10 20:11:02.120 3434-3434/com.example.deletemeapp D/skia: --- Failed to create image decoder with message 'unimplemented'

What you want to do is create the notification icon as follows:
NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this)
.setContentTitle("New Book Added")
.setContentText("Android with java");
Now Add:
NotificationCompat.BigPictureStyle s = new NotificationCompat.BigPictureStyle().bigPicture(bitmap_image);
s.setSummaryText("Summary text appears on expanding the notification");
//Set the style of the notification to this big picture style
builder.setStyle(s);
For the versioning add this:
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1) {
// Marshmallow+
}else{
//below Marshmallow
}
Hope this helps :)

Related

Notification comes in notification list but not pop ups on main screen

I am sharing two images please have a look on it you will understand my problem
in fist image notification comes successfully as shown better me notification but
I want it to come and show first on main screen as shown in second image just this telegram notification.
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification);
// r.play();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
r.setLooping(false);
}
// vibration
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
long[] pattern = {100, 300, 300, 300};
v.vibrate(pattern, -1);
int resourceImage = getResources().getIdentifier(remoteMessage.getNotification().getIcon(), "drawable", getPackageName());
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "CHANNEL_ID");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
builder.setSmallIcon(R.mipmap.betterme);
} else {
builder.setSmallIcon(R.mipmap.betterme);
}
Intent resultIntent = new Intent(this, SplashScreen.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 1, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentTitle(remoteMessage.getNotification().getTitle());
builder.setContentText(remoteMessage.getNotification().getBody());
builder.setContentIntent(pendingIntent);
builder.setStyle(new NotificationCompat.BigTextStyle().bigText(remoteMessage.getNotification().getBody()));
builder.setAutoCancel(true);
builder.setOngoing(true);
builder.setPriority(NotificationCompat.PRIORITY_MAX);
builder.setSound(notification);
mNotificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
String channelId = "Your_channel_id";
NotificationChannel channel = new NotificationChannel(
channelId,
"Channel human readable title",
NotificationManager.IMPORTANCE_HIGH);
AudioAttributes audioAttributes = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.build();
channel.setSound(notification, audioAttributes);
mNotificationManager.createNotificationChannel(channel);
builder.setChannelId(channelId);
}
mNotificationManager.notify(100, builder.build());
}
}
first image
second image
If I understand correctly, you would like to have a heads up notification.
Note, that the android system decides when to make a notification a heads up notification and has the final say - not you the developer. Here you can find some examples when it does so: https://developer.android.com/guide/topics/ui/notifiers/notifications#Heads-up
Make sure that your setup reflects these. From your sample it seems to be the case, but maybe you have changed the notification channel settings (from the app settings), which override your code preferences (the user has precedence over the app).
Additionally, note that if you swipe the heads up notification in a upward direction (not sideways), Android starts a cool off time, where no heads up notifications from that app appears for a few seconds (or more). You can try that with Telegram or any other app as well. After the cool-off time, it starts showing up again like a heads up notification. This is a way Android utilises to prevent apps to be annoying to users.
Seems there is no problem in the notification. But your channel is already created with normal notification and you update to IMPORTANCE_HIGH for NotificationChannel.
Once the channel is created with priority it cannot be changed. So you can either change the channel id or uninstall and reinstall and test it.

How to change the notification settings in android apps?

How can I switch all these settings on programmatically?
I noticed when you install WhatsApp they are all switched on in the beginning(look at the image below).
But I can not find a way to turn them on programmatically.
Here is how I send notifications:
private void sendNotification(Intent intent){
Context context = NotificationService.this;
//open the activity after the notification is clicked
Intent intent1 = new Intent(getApplicationContext(),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);
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
//These are necessary for the notification to pop up
if(Build.VERSION.SDK_INT < Build.VERSION_CODES.O){
builder.setPriority(Notification.PRIORITY_MAX);
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);
if(alarmSound != null){
AudioAttributes att = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.build();
channel.setSound(alarmSound,att);
}
channel.setLightColor(Color.BLUE);
channel.enableVibration(true);
channel.setDescription("Hello Dear friends"); //this is to test what this is
channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
channel.setVibrationPattern(new long[]{300, 300, 300});
notificationManager.createNotificationChannel(channel);
builder.setChannelId(channelId);
}
Notification notification = builder.build();
notificationManager.notify(0, notification);
}
I also added this permission to manifest:
<uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY" />
Update:
Using this code on the emulator, I get the heads-up notification. But on my Xiaomi device, there is no heads-up notification. It just appears on the status bar. If I manually turn on the floating notification (which you can see in the photo) then I will get heads-up notification. But they are switched off by default. When you install Whatsapp they are all switched on.
Is that a kind of privilege for Whatsapp as it is famout? or is there a way to do it?
By default, when you install a app, the system register's default notification channel (on low priority) that doesn't support head up notification by default, it's turned off. You can't control that.
But what you can do it create your own notification channel with highest priority and then register it on app run once.
After that just pass the channel Id with your notification builder so that system shows the head's up notification which you want.
More information can be found here https://developer.android.com/training/notify-user/channels
I have tried for this but unable to set these settings programmatically. Instead of this I have used following method to open notification settings screen to enable notification sounds/vibration.
private void openNotificationSettingsForApp(String channelId) {
// Links to this app's notification settings.
Intent intent = new Intent();
intent.setAction("android.settings.APP_NOTIFICATION_SETTINGS");
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && channelId!=null){
intent.setAction(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS);
intent.putExtra(Settings.EXTRA_CHANNEL_ID,channelId);
intent.putExtra("android.provider.extra.APP_PACKAGE", getPackageName());
}
intent.putExtra("app_package", getPackageName());
intent.putExtra("app_uid", getApplicationInfo().uid);
startActivity(intent);
}

Cannot enable blinking light and disable notification sound for Oreo notifications

I am working with AlarmManager, and when the alarm starts it also shows notifications. I have created notifications for Oreo and for before Oreo. Notifications before Oreo work properly - I can disable sounds and set lights, but I cannot make this work in Oreo. I had the similar issue with vibrations, but was able to find a working solution. I have tried A LOT of things (1, 2, 3, 4, 5, 6...), from NotificationCompat, to changing importance, but was unable to make it work.
My issue is only with Oreo notifications, I cannot disable sound (it goes of every time), and I cannot make light blink. I have went through a bunch of SO questions, and official documentation. Some solutions are obsolete, deprecated (NotificationCompat.Builder), others do not work at all (including some examples from the official documentation).
Here is my code for both Oreo (not working) and for older (working):
//region Oreo notifications
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = "AlarmNotification";
String description = "Alarm notification";
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel mChannel = new NotificationChannel(channelIdOreo, name, importance);
mChannel.setDescription(description);
mChannel.setShowBadge(true);
mChannel.enableLights(true);
mChannel.setLightColor(Color.RED);
if (notificationManager != null) {
notificationManager.createNotificationChannel(mChannel);
}
Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
if (sNotifications.equals("false")) {
//NOT WORKING
mChannel.setSound(null, null);
}
//VIBRATION WORKING
if (sVibration.equals("true")) {
if (vibrator != null && vibrator.hasVibrator()) {
VibrationEffect effect = VibrationEffect.createOneShot(1000, VibrationEffect.DEFAULT_AMPLITUDE);
vibrator.vibrate(effect);
}
}
Notification notification = new Notification.Builder(context, channelIdOreo)
.setContentTitle(contentTitleText)
.setContentText(contentContentText)
.setNumber(1)
.setSmallIcon(whiteLogo)
.setBadgeIconType(whiteLogo)
.setAutoCancel(true)
.setContentIntent(pendingIntent)
.build();
if (notificationManager != null) {
notificationManager.notify(notificationCode, notification);
}
}
//endregion
//region Pre-Oreo notifications
else {
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context, channelId)
.setSmallIcon(whiteLogo)
.setLargeIcon(largeIcon)
.setContentTitle(contentTitleText)
.setContentText(contentContentText)
.setOngoing(false)
.setNumber(1)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setAutoCancel(true);
mBuilder.setLights(colorPPDOrange, 1000, 2000);
if (sNotifications.equals("true")) {
mBuilder.setSound(uri);
}
if (sVibration.equals("true")) {
mBuilder.setVibrate(new long[]{1000, 1000, 1000, 1000, 1000});
}
mBuilder.setContentIntent(pendingIntent);
if (notificationManager != null) {
notificationManager.notify(notificationCode, mBuilder.build());
}
}
//endregion
Finally found an answer. Every time I change anything related to my channel I had to have my channel id changed to a completely new id, that was NEVER used before. It cannot just be different from the current id. Other than this I replaced my string resource with actual string in code. Also I had to move a few lines of code as shown below.
This is how my code looks like now:
String channelIdOreo = "FfffffF";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//region Oreo otification
Notification notification = new Notification.Builder(context, channelIdOreo)
.setContentTitle(contentTitleText)
.setContentText(contentContentText)
.setNumber(1)
.setSmallIcon(whiteLogo)
.setBadgeIconType(whiteLogo)
.setAutoCancel(true)
.setContentIntent(pendingIntent)
.build();
//endregion
NotificationChannel mChannel = new NotificationChannel(channelIdOreo, "Channel human readable title and stuff", NotificationManager.IMPORTANCE_DEFAULT);
mChannel.enableLights(true);
mChannel.setLightColor(Color.YELLOW);
//region Conditions from settings
if (sNotifications.equals("true")) {
mChannel.setSound(uri, null);
} else {
mChannel.setSound(null, null);
}
if (sVibration.equals("true")) {
mChannel.setVibrationPattern(new long[]{1000, 1000, 1000, 1000, 1000});
} else {
mChannel.enableVibration(false);
}
//endregion
if (notificationManager != null) {
notificationManager.createNotificationChannel(mChannel);
}
if (notificationManager != null) {
notificationManager.notify(notificationCode, notification);
}
}
Hope this will save some time to someone.

Add a custom notification led light

I have a phone under Android 8 and I begin Android programmation with Android Studio. Some apps like Snapchat or Facebook makes my phone light a led with their custom colors (yellow & blue) when a notification comes.
I want to do the same with my app, I searched a lot and nothing works, the notification appears but not the white light. I checked my phone settings and my app is allowed to light the led.
public class Notification
{
public Notification(String channelId, String channelName, Context context, String title, String body, Intent intent)
{
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
int notificationId = 1;
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);
}
NotificationCompat.Builder nbuilder = new NotificationCompat.Builder(context, channelId)
.setLights(Color.WHITE, 500, 100) // doesn't work
.setVibrate(new long[] { 1000, 1000, 1000, 1000, 1000 }) // doesn't work
.setContentTitle(title)
.setContentText(body);
TaskStackBuilder stackBuilder =
TaskStackBuilder.create(context);
stackBuilder.addNextIntent(intent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
nbuilder.setContentIntent(resultPendingIntent);
notificationManager.notify(notificationId, nbuilder.build());
}
}
I tried calling the setLights() and setVibrate() after setContentText() and I tried calling these voids just before the notify() but it didn't changed anything.
I instanciate my class in the onCreate() :
new Notification("channel-01", "MathWellan Notification", this, "Twitter", "Follow me on Twitter !", new Intent(Intent.ACTION_VIEW, Uri.parse("twitter://user?screen_name=MathWellan")));
Sorry for my bad english, I'm french and I hope you can help me !
Thanks by advance :)
Use this line of code to change notification LED color: your_notification.ledARGB = Color.YOUR_COLOR;
Usage example:
NotificationManager notif = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notif.cancel(1); // clear previous notification
final Notification notification = new Notification();
notification.ledARGB = Color.MAGENTA;
notification.ledOnMS = 1000;
notification.flags |= Notification.FLAG_SHOW_LIGHTS;
notif.notify(1, notification);
Note : screen should be locked when you test it , because notification LED will be highlighted only when screen is off. Maybe that's your problem.

(Duplicate) Android studio, notification won't appear [duplicate]

I get this message when trying to display a notification on Android O.
Use of stream types is deprecated for operations other than volume
control
The notification is straight from the example docs, and displays fine on Android 25.
Per the comments on this Google+ post:
those [warnings] are currently expected when using NotificationCompat on Android O devices (NotificationCompat always calls setSound() even if you never pass in custom sound).
until the Support Library changes their code to use the AudioAttributes version of setSound, you'll always get that warning.
Therefore there's nothing that you can do about this warning. As per the notification channels guide, Android O deprecates setting a sound on an individual notification at all, instead having you set the sound on a notification channel used by all notifications of a particular type.
Starting with Android O, you are required to configure a NotificationChannel, and reference that channel when you attempt to display a notification.
private static final int NOTIFICATION_ID = 1;
private static final String NOTIFICATION_CHANNEL_ID = "my_notification_channel";
...
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "My Notifications", NotificationManager.IMPORTANCE_DEFAULT);
// Configure the notification channel.
notificationChannel.setDescription("Channel description");
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
notificationChannel.enableVibration(true);
notificationManager.createNotificationChannel(notificationChannel);
}
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
.setVibrate(new long[]{0, 100, 100, 100, 100, 100})
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Content Title")
.setContentText("Content Text");
notificationManager.notify(NOTIFICATION_ID, builder.build());
A couple of important notes:
Settings such as vibration pattern specified in the NotificationChannel override those specified in the actual Notification. I know, its counter-intuitive. You should either move settings that will change into the Notification, or use a different NotificationChannel for each configuration.
You cannot modify most of the NotificationChannel settings after you've passed it to createNotificationChannel(). You can't even call deleteNotificationChannel() and then try to re-add it. Using the ID of a deleted NotificationChannel will resurrect it, and it will be just as immutable as when it was first created. It will continue to use the old settings until the app is uninstalled. So you had better be sure about your channel settings, and reinstall the app if you are playing around with those settings in order for them to take effect.
All that #sky-kelsey has described is good, Just minor additions:
You should not register same channel every time if it has been already registered, so I have Utils class method that creates a channel for me:
public static final String NOTIFICATION_CHANNEL_ID_LOCATION = "notification_channel_location";
public static void registerLocationNotifChnnl(Context context) {
if (Build.VERSION.SDK_INT >= 26) {
NotificationManager mngr = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
if (mngr.getNotificationChannel(NOTIFICATION_CHANNEL_ID_LOCATION) != null) {
return;
}
//
NotificationChannel channel = new NotificationChannel(
NOTIFICATION_CHANNEL_ID_LOCATION,
context.getString(R.string.notification_chnnl_location),
NotificationManager.IMPORTANCE_LOW);
// Configure the notification channel.
channel.setDescription(context.getString(R.string.notification_chnnl_location_descr));
channel.enableLights(false);
channel.enableVibration(false);
mngr.createNotificationChannel(channel);
}
}
strings.xml:
<string name="notification_chnnl_location">Location polling</string>
<string name="notification_chnnl_location_descr">You will see notifications on this channel ONLY during location polling</string>
And I call the method every time before I'm going to show a notification of the type:
...
NotificationUtil.registerLocationNotifChnnl(this);
return new NotificationCompat.Builder(this, NotificationUtil.NOTIFICATION_CHANNEL_ID_LOCATION)
.addAction(R.mipmap.ic_launcher, getString(R.string.open_app),
activityPendingIntent)
.addAction(android.R.drawable.ic_menu_close_clear_cancel, getString(R.string.remove_location_updates),
servicePendingIntent)
.setContentText(text)
...
Another typical problem - channel default sound - described here: https://stackoverflow.com/a/45920861/2133585
In Android O it's a must to use a NotificationChannel and NotificationCompat.Builder is deprecated (reference).
Below is a sample code :
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(mContext.getApplicationContext(), "notify_001");
Intent ii = new Intent(mContext.getApplicationContext(), RootActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0, ii, 0);
NotificationCompat.BigTextStyle bigText = new NotificationCompat.BigTextStyle();
bigText.bigText(verseurl);
bigText.setBigContentTitle("Today's Bible Verse");
bigText.setSummaryText("Text in detail");
mBuilder.setContentIntent(pendingIntent);
mBuilder.setSmallIcon(R.mipmap.ic_launcher_round);
mBuilder.setContentTitle("Your Title");
mBuilder.setContentText("Your text");
mBuilder.setPriority(Notification.PRIORITY_MAX);
mBuilder.setStyle(bigText);
NotificationManager mNotificationManager =
(NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel("notify_001",
"Channel human readable title",
NotificationManager.IMPORTANCE_DEFAULT);
mNotificationManager.createNotificationChannel(channel);
}
mNotificationManager.notify(0, mBuilder.build());

Categories