How to set notification to clear itself on click? - android

How to set my notification to clear itself on click?
I've set autoCancel(true) but it doesn't work
My code:
Notification n = new Notification.Builder(this)
.setContentTitle("Update for you")
.setContentText("Please click here to see the update information")
.setLargeIcon(BitmapFactory.decodeResource(this.getResources(),
R.drawable.ic_launcher))
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pList)
.setAutoCancel(true)
.addAction(R.drawable.ic_read, "Read", pRead)
.addAction(R.drawable.ic_list, "All Updates", pList)
.build();
NotificationManager notificationManager =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, n);

setAutoCancel() of Notification.Builder is introduced in API 11. Perhaps, your minimum version is lower than API 11.
setAutoCancel() is also available in NotificationCompat.Builder class which is added in Support library
Notification n = new NotificationCompat.Builder(this)
.setContentTitle("Update for you")
.setContentText("Please click here to see the update information")
.setLargeIcon(BitmapFactory.decodeResource(this.getResources(),
R.drawable.ic_launcher))
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pList)
.setAutoCancel(true)
.addAction(R.drawable.ic_read, "Read", pRead)
.addAction(R.drawable.ic_list, "All Updates", pList)
.build();

Use FLAG_AUTO_CANCEL
n.flags |= Notification.FLAG_AUTO_CANCEL;

SetAutoCancel(true)
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
// .setLargeIcon(image)/*Notification icon image*/
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle("Quiz Tap")
.setContentText(messageBody)
/*.setStyle(new NotificationCompat.BigPictureStyle()
.bigPicture(image))*//*Notification with Image*/
**.setAutoCancel(true)**
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);

Related

Notification couldn't disappear after click, and Notification setAutoCancel(true) doesn't work

Notification couldn't disappear after click, and Notification setAutoCancel(true) doesn't work. The following is my code:
NotificationManager notifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("New Message")
.setDefaults(Notification.DEFAULT_ALL)
.setContentText("New Question")
.setAutoCancel(true);
notifyManager.notify(1, builder.build());
Try setContentIntent():
PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0, new Intent(), 0);
NotificationManager notifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("New Message")
.setDefaults(Notification.DEFAULT_ALL)
.setContentText("New Question")
.setContentIntent(pendingIntent)
.setAutoCancel(true);
notifyManager.notify(1, builder.build());

Android - notification does not appear in status bar

When starting an IntentService to upload a file in the background, I want to show a notification to the user that the upload is in progress by calling showNotification() from inside my service:
private void showNotification() {
Notification notification = new Notification.Builder(this)
.setSmallIcon(R.drawable.ic_cloud_upload_black_24dp)
.setWhen(System.currentTimeMillis())
.setContentTitle("Uploading")
.setContentText("Your upload is in progress.")
.setOngoing(true)
.build();
mNotificationManager.notify(NOTIFICATION_ID, notification);
}
Now here's my problem: the notification appears on the lock screen, but not in the status bar when the screen is unlocked. What am I missing?
Deployment target is API level 24, so a missing NotificationChannel should not be the cause.
try it :
PendingIntent pendingIntent = PendingIntent.getActivity(getmContext(), 0, new Intent(getmContext(), MainActivity.class), 0);
android.app.Notification pp = new NotificationCompat.Builder(getmContext())
.setSmallIcon(R.drawable.ic_launcher_background)
.setContentText(getMessage())
.setContentIntent(pendingIntent)
.build();
NotificationManager notificationManager = (NotificationManager) getmContext().getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, pp);
This is my code which is working fine with target SDK 25
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
mContext);
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
inboxStyle.addLine(message);
Notification notification;
notification = mBuilder.setSmallIcon(icon).setTicker(title).setWhen(0)
.setAutoCancel(true)
.setContentTitle(title)
.setContentIntent(resultPendingIntent)
.setStyle(inboxStyle)
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(), icon))
.setContentText(message)
.build();
NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICATION_ID, notification);
where NOTIFICATION_ID
public static final int NOTIFICATION_ID = 100;
I feel so dumb. Turns out the notification was displayed all the time, but with a black icon on black background.
Changed the icon colour in the xml from
android:fillColor="#FF000000"
to
android:fillColor="#FFFFFFFF"
and it works like a charm

notification setLargeIcon and setSmallIcon not working

I am trying to setLargeIcon & setSmallIcon but it's not working.
Notification is showing but default android icon is being shown.
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, "my_channel_01")
.setContentIntent(pendingIntent)
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.app_icon))
.setSmallIcon(R.drawable.app_icon)
.setContentTitle("Notification Title")
.setContentText("Notification Text")
//.setPriority(Notification.PRIORITY_MAX)
.setSound(alarmSound)
.setAutoCancel(true);

NotificationCompat.Builder with expand notification is not working

I am trying to expand notification if text is long by below code.
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(getNotificationIcon())
.setColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary))
.setContentTitle(getString(R.string.app_name))
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setStyle(new NotificationCompat.BigTextStyle().bigText(messageBody))
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
but it shows single line notification and cannot expand it. My testing device is the Marshmallow.
Using setpriority might help-
.setPriority(Notification.PRIORITY_MAX)

setgroup() in notification not working

I'm tring to create notification group, this is my code:
// Build the notification, setting the group appropriately
Notification notif = new NotificationCompat.Builder(getApplicationContext())
.setContentTitle("New mail from " + 1)
.setContentText("cv")
.setSmallIcon(R.drawable.rh_logo)
.setStyle(new NotificationCompat.InboxStyle()
.addLine("Alex Faaborg Check this out")
.addLine("Jeff Chang Launch Party")
.setBigContentTitle("2 new messages")
.setSummaryText("johndoe#gmail.com"))
.setGroup(GROUP_KEY_EMAILS)
.setGroupSummary(true)
.build();
// Issue the notification
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(++NOTIFICATION_ID, notif);
When I run the app, and send notification messages, they do not show in group.
Can someone explain me what I need to change?
You have to create a group notification before creating your custom notification. Just like this:
NotificationCompat.Builder groupBuilder =
new NotificationCompat.Builder(context)
.setContentTitle(title)
.setContentText(content)
.setGroupSummary(true)
.setGroup("GROUP_1")
.setStyle(new NotificationCompat.BigTextStyle().bigText(content))
.setContentIntent(pendingIntent);
Do not forget setGroupSummary to true.
Then create your child notification which group value is same to groupBuilder's value。Here is "GROUP_1".
NotificationCompat.Builder builder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_stat_communication_invert_colors_on)
.setContentTitle(title)
.setContentText(content)
.setGroup("GROUP_1")
.setStyle(new NotificationCompat.BigTextStyle().bigText(content))
.setContentIntent(pendingIntent)
Finally use NoticationManagerCompat to notify them.
NotificationManagerCompat manager = NotificationManagerCompat.from(context);
manager.notify(GROUP_ID, groupBuilder.build());
manager.notify(id, builder.build());
Replace
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(++NOTIFICATION_ID, notif);
with
NotificationManagerCompat.from(mCtx).notify(++NOTIFICATION_ID,notif);
because you are using NotificationCompat
There is a strange behavior of notification. If you add autocancel notification group doesn't work, if you add setGroup or setGroupSummary. It will not work. Removing all that fixed the problem for me. Basically, you don't need to mention any group it will auto group. TESTED on Redmi 10 pr0, Poco x3 NFC, huawei device.
return new NotificationCompat.Builder(context, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_action_name)
.setContentTitle(title)
.setContentText(body)
.setStyle(new NotificationCompat.BigTextStyle().bigText(body))
.setPriority(NotificationCompat.PRIORITY_LOW);

Categories

Resources