Notification in Nougat - java

I have two problem with my app:
1. I don't know how to retrieve the color of the small icon of notification when a notification arrives?
2. Can I edit app name that displayed at top of the notification by programmatically?
I'm using android Nougat .

You can use the below code to set smallIcon :(Use Small icon same color as Title Color)
Change color by this site
.setSmallIcon(R.drawable.ic_launcher_icon)
Reference Code :
private void sendNotification(String messageBody,String message2, Bitmap image, String TrueOrFalse) {
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("AnotherActivity", TrueOrFalse);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
// .setLargeIcon(image)/*Notification icon image*/
.setLargeIcon(BitmapFactory.decodeResource(this.getResources(), R.drawable.ic_launcher_icon))
.setSmallIcon(R.drawable.ic_launcher_icon)
.setContentTitle(messageBody)
.setContentText(message2)
.setColor(getResources().getColor(R.color.colorAccent))
.setStyle(new NotificationCompat.BigPictureStyle()
.bigPicture(image))/*Notification with Image*/
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}

This API seems to allow custom title and icon: https://developer.android.com/reference/android/app/Notification.Builder.html

Related

Notification LED not turning on

I tried almost every fix posted here but this code won't turn on Notification LED at all.
public void onMessageReceived(RemoteMessage remoteMessage) {
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_ONE_SHOT);
String channelId = getString(R.string.defaultChannelId);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this,channelId)
.setLights(Color.RED,200,1000)
.setPriority(NotificationCompat.PRIORITY_MAX)
.setAutoCancel(true)
.setDefaults(NotificationCompat.DEFAULT_ALL)
.setContentTitle(remoteMessage.getNotification().getTitle())
.setContentText(remoteMessage.getNotification().getBody())
.setSmallIcon(R.drawable.omega_icon)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
Notification notification = notificationBuilder.build();
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelId,
"Channel human readable title",
NotificationManager.IMPORTANCE_DEFAULT);
channel.enableLights(true);
channel.setLightColor(0xFFFFFFFF);
notificationManager.createNotificationChannel(channel);
}
notificationManager.notify(1, notification);
}
I'm sending notification from Firebase and everything works perfectly fine except the LED lights. LED notification from other apps work fine.
You can't change channel color notification after Oreo/API 26.
developer.android.com

Notification cancelled immediately on Samsung Galaxy S6 android 7

I created a notification using this code:
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
final int notificationID = NotificationID.getID();
PendingIntent pendingIntent = PendingIntent.getActivity(this, notificationID, intent, PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notif_icon)
.setContentTitle(messageTitle)
.setStyle(new NotificationCompat.BigTextStyle().bigText(messageTitle + "\n" +messageBody))
.setContentText(messageTitle + " " +messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(notificationID, notificationBuilder.build());
This works well on many devices, however on a Samsung Galaxy S6 android 7 the notification is cancelled immediately.
I tried putting the ongoing flag and the notification remains visible, however this is not a solution because the user won't be able to cancel it be swiping the notification.
Any idea what might cause the problem?
Remove setAutoCancel(true) from your code.
setAutoCancel()
Notification.Builder setAutoCancel (boolean autoCancel) Make this
notification automatically dismissed when the user touches it.
Reference: https://developer.android.com/reference/android/app/Notification.Builder.html#setAutoCancel(boolean)

How to show just notification count on appview android

I'm new to android. And to show notification on android phones I got this below code
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /*Request code*/ , intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.mytune_default)
.setContentTitle("FCM Message")
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
int m = (int) ((new Date().getTime() / 1000L) % Integer.MAX_VALUE);
notificationManager.notify(m /*ID of notification*/ , notificationBuilder.build());
But the thing is I just want that notification unread count on code appview on system, but dont want other things loke message, title etc that appear on notification tray of system. How come this be acheived>
It's not possible to replace launcher icon with widget. You can say to user to place your widget on the home screen, but not in application list. Users must do that in their own. Nevertheless, it's iOs pattern. It's not good idea to bring iOS design elements to android. Android have great notification bar, where user expect to see notification from your app. Android users don't expect to see it over the icon.

Show notification in Android 5.0.1

I want to create a Notification in my Android App, but my code doesn't work!
NotificationManager nManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification.Builder nBuilder = new Notification.Builder(this);
nBuilder.setContentTitle("Title...");
nBuilder.setContentText("Text...");
nBuilder.setContentInfo("Info?");
nBuilder.setWhen(System.currentTimeMillis());
//Intent notificationIntent = new Intent(this, MainActivity.class);
//PendingIntent pendingIntent = new PendingIntent.getActivity(this, 0, notificationIntent, 0);
nManager.notify(noti_ID, nBuilder.build());
Thanks :)
From developer.android.com
Required notification contents
A Notification object must contain the following:
A small icon, set by setSmallIcon()
A title, set by setContentTitle()
Detail text, set by setContentText()
So set a small icon too.

make notification clickable and undestroyable

I tried to create a notification that starts a activity by clicking on it and which you can't swipe away.
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher))
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("DroidSchool")
.setContentText("DroidSchool l\u00E4uft im Hintergrund...");
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, Intent.FLAG_FROM_BACKGROUND);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
int mId = 1234567890;
mNotificationManager.notify(mId, mBuilder.build());
with the code above the notification gets displayed, but nothing happens when I click on it and you can swipe it away.
To keep the Notification, in that way the user can't click it away in any way, add this flag
Notification mNotification = mBuilder.build();
notification.flags = Notification.FLAG_NO_CLEAR;
mNotificationManager.notify(mId, notification);
To start an Activity you have to use this flag for your Intent
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
Do like this:
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher))
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("DroidSchool")
.setContentText("DroidSchool l\u00E4uft im Hintergrund...");
Intent intent = new Intent(YourActivity.this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
Notification noti = mBuilder.build();
noti.flags |= mBuilder.build().FLAG_NO_CLEAR | mBuilder.build().FLAG_ONGOING_EVENT;
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(0, noti);
You want a foreground service as detailed here
It's worth noting that only android 4.3 added the persistent notification you seem to be after and it can be overriden by the users settings.
try using like this
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, Intent.FLAG_UPDATE_CURRENT);
in MainActivity.java
Dont not consume your notification; ie instead of calling below 2 line code in oncreate() call under onDestro()
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
nm.cancel(mId);
You can use NotificationCompat.Builder's .setOngoing(true), use it like this:
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setAutoCancel(true)
.setContentTitle("Downloading Something")
.setContentText("Downloading")
.setSound(null)
.setDefaults(0)
.setOngoing(true);
// Makes it so the notification is clickable but not destroyable by swiping-away or hitting "clear all"

Categories