Delete notification notification.builder on content click - android

I have a notification that have 2 actions. One intent open an Activity, the other open browser web.
my code:
Intent intent1 = new Intent(context, NotificationClickActivity.class);
PendingIntent pIntent1 = PendingIntent.getActivity(context, 0, intent1, PendingIntent.FLAG_CANCEL_CURRENT);
String url = "http://www.google.com";
Intent browserIntent = new Intent(Intent.ACTION_VIEW);
browserIntent.setData(Uri.parse(url));
PendingIntent pBrowserIntent = PendingIntent.getActivity(context, 1, browserIntent, PendingIntent.FLAG_CANCEL_CURRENT);
// Build notification
Notification noti = new Notification.Builder(context)
.setContentTitle(title)
.setContentText(shortDescription)
.setStyle(new Notification.BigTextStyle().bigText(longDescription))
.setSmallIcon(R.drawable.small_defaulticon)
.setAutoCancel(true)
.setContentIntent(pIntent1)
.addAction(R.drawable.playstore_icon_32, "Dettagli", pIntent1)
.setContentIntent(pBrowserIntent)
.addAction(R.drawable.small_defaulticon, "Vai al sito", pBrowserIntent).build();
NotificationManager notificationManager =
(NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
// Hide the notification after its selected
noti.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, noti);
If i click on the notification it goes away, but if I click on one of the 2 Actions it open the right intent but notification doesn't cleared.
I try also .setDeleteIntent(myPendingIntent) but nothing... I wonder where I am going wrong...
A screen of my notification...
Thank you!

A notification is never dismissed when an action button is pressed. It just sends you associated intent. If you want it to dismiss, you need to call NoficationManager.cancel(int id) to cancel it yourself. You will normally do it in the same method, which handles action button intent.
Notification.FLAG_AUTO_CANCEL flag you tried to use is only applied to notification body and not to action buttons.

use cancel() in the activity that started by your action to dismiss the notification

try this
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setDefaults(Notification.DEFAULT_ALL);
and try to send uniqueId in notificationManager.notify(uniqueId, noti);

Related

Notification is not being dismissed (Android)

Notification setAutoCancel(true) doesn't work if clicking on Action
I have a notification with an action within it. When I tap on the notification it gets removed from the list. However, when I click on the Action it successfully completes the Action (namely makes a call), but when I return to the list of notifications, it remains there.
Relative code of the AlarmReceiver:
public class AlarmReceiver extends BroadcastReceiver {
Meeting meeting;
/**
* Handle received notifications about meetings that are going to start
*/
#Override
public void onReceive(Context context, Intent intent) {
// Get extras from the notification intent
Bundle extras = intent.getExtras();
this.meeting = extras.getParcelable("MeetingParcel");
// build notification pending intent to go to the details page when click on the body of the notification
Intent notificationIntent = new Intent(context, MeetingDetails.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
notificationIntent.putExtra("MeetingParcel", meeting); // send meeting that we received to the MeetingDetails class
notificationIntent.putExtra("notificationIntent", true); // flag to know where the details screen is opening from
PendingIntent pIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
// build intents for the call now button
Intent phoneCall = Call._callIntent(meeting);
if (phoneCall != null) {
PendingIntent phoneCallIntent = PendingIntent.getActivity(context, 0, phoneCall, PendingIntent.FLAG_CANCEL_CURRENT);
int flags = Notification.FLAG_AUTO_CANCEL;
// build notification object
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
Notification notification = builder.setContentTitle("Call In")
.setContentText(intent.getStringExtra("contextText"))
.setTicker("Call In Notification")
.setColor(ContextCompat.getColor(context, R.color.colorBluePrimary))
.setAutoCancel(true) // will remove notification from the status bar once is clicked
.setDefaults(Notification.DEFAULT_ALL) // Default vibration, default sound, default LED: requires VIBRATE permission
.setSmallIcon(R.drawable.icon_notifications)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(meeting.description))
.addAction(R.drawable.icon_device, "Call Now", phoneCallIntent)
.setCategory(Notification.CATEGORY_EVENT) // handle notification as a calendar event
.setPriority(Notification.PRIORITY_HIGH) // this will show the notification floating. Priority is high because it is a time sensitive notification
.setContentIntent(pIntent).build();
notification.flags = flags;
// tell the notification manager to notify the user with our custom notification
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notification);
}
}
}
use this flag:
Notification.FLAG_AUTO_CANCEL
inside this:
int flags = Notification.FLAG_AUTO_CANCEL;
Notification notification = builder.build();
notification.flags = flags;
Documentation
Ok turns out it's a known problem already, and it needs extra code to be done (keeping reference to notification through id). Have no clue why API does not provide this, as it seems very logical to do. But anyways,
see this answer in stackoverflow:
When you called notify on the notification manager you gave it an id - that is the unique id you can use to access it later (this is from the notification manager:
notify(int id, Notification notification)
To cancel, you would call:
cancel(int id)
with the same id. So, basically, you need to keep track of the id or possibly put the id into a Bundle you add to the Intent inside the PendingIntent?
I faced this problem today, and found that FLAG_AUTO_CANCEL and setAutoCanel(true), both work when click on notification,
but not work for action click
so simply, in the target service or activity of action, cancel the notification
NotificationManager manager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
manager.cancelAll();
or if have more notification
manager.cancel(notificationId);
You have created two pending intent use in boths and change Flag too.
PendingIntent pIntent = PendingIntent.getActivity(context, (int) System.currentTimeMillis(), notificationIntent, 0);
PendingIntent phoneCallIntent = PendingIntent.getActivity(context, (int) System.currentTimeMillis(), phoneCall, PendingIntent.FLAG_UPDATE_CURRENT);
// CHANGE TO THIS LINE
PendingIntent pIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
PendingIntent phoneCallIntent = PendingIntent.getActivity(context, 0, phoneCall, PendingIntent.FLAG_CANCEL_CURRENT);

How can I click missed calls icon to open system Missed Calls UI?

I can use the following code to display a notification icon of missed calls, I hope to click the icon to open system Missed Calls UI, how can I do ? Thanks!
At present, I can open ui.CallerMain.class UI if I remove the comment.
BTW, in system Missed Calls UI, missed calls are listed in there.
private void ShowMissCallNotification(Context myContext,String myContentText) {
NotificationManager notificationManager = (NotificationManager) myContext.getSystemService(android.content.Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(android.R.drawable.sym_call_missed,
myContext.getString(R.string.app_name),
System.currentTimeMillis());
notification.flags |= Notification.FLAG_ONGOING_EVENT;
notification.flags |= Notification.FLAG_NO_CLEAR;
CharSequence contentTitle= "Title";
CharSequence contentText =myContentText;
//Intent notificationIntent = new Intent(myContext, ui.CallerMain.class);
//PendingIntent contentItent = PendingIntent.getActivity(myContext, 0, notificationIntent, 0);
//notification.setLatestEventInfo(myContext, contentTitle, contentText,contentItent);
notificationManager.notify(NotificationID, notification);
}
Set a Pending Intent to the notification which will trigger the Call History.
First create an intent with Call Log
Intent resultIntent = new Intent();
resultIntent.setAction(Intent.ACTION_VIEW);
resultIntent.setType(CallLog.Calls.CONTENT_TYPE);
Then obtain the PendingIntent
PendingIntent resultPendingIntent =
PendingIntent.getActivity(
this,
0,
resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT
);
Then set the PendingIntent to your notification builder
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon()
.setContentTitle()
.setContentText()
.setContentIntent(resultPendingIntent);
notificationManager.notify(id, builder.build());
Now clicking the notification will open the Call Log.
Update: The code commented out in your snippet will work if you create the Intent as mentioned above in this answer. But please be aware that the method by which you are creating notification has been deprecated. Use NotificationCompat class from the support library in future.

How to add a Dynamic image instead of notification icon in android?

I used below code for displaying notification in notification Bar.
it worked fine.
But i need to display notification icon dynamically that will come from web service.
How i can do?
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification note = new Notification(R.drawable.image,"status message", System.currentTimeMillis());
Intent in = new Intent(Notify.this,CommentU.class);
PendingIntent pi = PendingIntent.getActivity(Notify.this, 0, in, 0);
note.setLatestEventInfo(Notify.this,"NotificationTitle", "You have a new Commentio", pi);
note.number = ++count;
note.vibrate = new long[] { 500l, 200l, 200l, 500l };
note.flags |= Notification.FLAG_AUTO_CANCEL;
nm.notify(NOTIFY_ME_ID, note);
Thanks in Advance.
I have one suggestion for you: you have to download that image which you want to show and then you can set that as bitmap: check below code. I have created one BITMAP.
its look link :
for this you have to add android-support-v4.jar
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
this).setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("My notification").setLargeIcon(BITMAP)
.setContentText("Hello World!");
Intent resultIntent = new Intent(this, test.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(test.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(NOTIFY_ME_ID, mBuilder.build());
for more detail chekc this link.
Removing notifications
Notifications remain visible until one of the following happens:
The user dismisses the notification either individually or by using "Clear All" (if the notification can be cleared).
The user clicks the notification, and you called setAutoCancel() when you created the notification.
You call cancel() for a specific notification ID. This method also deletes ongoing notifications.
You call cancelAll(), which removes all of the notifications you previously issued.
Edited: just replace this.
mBuilder = new NotificationCompat.Builder(
this).setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("My notification").setLargeIcon(BITMAP)
.setAutoCancel(true)
.setContentText("Hello World!");
just add some icons in your resource folder and then
int myIcon = R.drawable.image;
your logic goes here ....
and change value of icon according to your logic like myIcon = R.drawable.somenewimage;
and then finally set your notification
Notification note = new Notification(myIcon,"status message", System.currentTimeMillis());

Perform an action clicking notifications in android

I am trying to create notifications correctly, but as I can not perform an action when you click on the notification. I hope you can help me or give some clue.
It is recommended to use Notification.Builder and NotificationCompat.Builder from the support library for building notifications.
Also why do use reuse the Intent which was caught by your BroadcastReceiver? It should refer to the actual activity which you want to launch by click.
For example:
Intent intent = new Intent(context, MainActivity.class);
Notification notification = new NotificationCompat.Builder(context)
.setContentTitle(contentTitle)
.setContentText(contentText)
.setContentIntent(PendingIntent.getActivity(context, 0, intent, 0))
.setAutoCancel(true)
.build();
If you doesn't really want to launch some activity, you may start a Service or send a broadcast message instead. See PendingIntent.
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notif = new Notification(R.drawable.ic_launcher, "Text",
System.currentTimeMillis());
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("somekey", "someextra");
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
notif.setLatestEventInfo(this, "Title", "Message", pIntent);
notif.flags |= Notification.FLAG_AUTO_CANCEL;
nm.notify(1, notif);
Use Notification.Builder for new version api

Android Notification intent to clear it self

I have read many examples of how to create notification messages.
What i wanted to achieve, is because the notification will be executed by a widget, i would like
the notification intent when clicked to clear it self when the user clicks on it.I do not have an activity to return to.
The notification for my purposes will just plainly notify, nothing else.
So what would be the code of an intent that just clear/cancel itself.
The code below is an activity launched by a button(button code not included) the notification will be fired up by a background service.
CharSequence title = "Hello";
CharSequence message = "Hello, Android!";
final NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
final Notification notification = new Notification(R.drawable.icon,"A New Message!",System.currentTimeMillis());
notification.defaults=Notification.FLAG_ONLY_ALERT_ONCE+Notification.FLAG_AUTO_CANCEL;
Intent notificationIntent = new Intent(this, AndroidNotifications.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,notificationIntent, 0);
notification.setLatestEventInfo(AndroidNotifications.this, title,message, pendingIntent);
notificationManager.notify(NOTIFICATION_ID, notification);
Thanks
Check out FLAG_AUTO_CANCEL
Bit to be bitwise-ored into the flags field that should be set if the notification should be canceled when it is clicked by the user.
EDIT :
notification.flags |= Notification.FLAG_AUTO_CANCEL;
Set the flags in notification.flags instead of notification.defaults.
Example:
notification.flags |= Notification.FLAG_ONLY_ALERT_ONCE | Notification.FLAG_AUTO_CANCEL;
If you're using NotificationCompat.Builder (a part of android.support.v4) then simply call its object's method setAutoCancel
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
builder.setAutoCancel(true);
Some guys were reporting that setAutoCancel() did not work for them, so you may try this way as well
builder.build().flags |= Notification.FLAG_AUTO_CANCEL;
The only way I can see of doing this is to have your Notification's Intent point to a background Service. When this Service is launched, it would clear the given Notification using NotificationManager.cancel(int id). The Service would then stop itself. It's not pretty, and would not be easy to implement, but I can't find any other way of doing it.
/**
Post a notification to be shown in the status bar.
Obs.: You must save this values somewhere or even pass it as an extra through Intent to use it later
*/
notificationManager.notify(NOTIFICATION_ID, notification);
/**
Cancel a previously shown notification given the notification id you've saved before
*/
notificationmanager.cancel(NOTIFICATION_ID);
Using setContentIntent should solve your problem:
.setContentIntent(PendingIntent.getActivity(this, 0, new Intent(), 0));
For example:
NotificationCompat.Builder mBuilder= new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("title")
.setAutoCancel(true)
.setContentText("content")
.setContentIntent(PendingIntent.getActivity(this, 0, new Intent(), 0));
NotificationManager notificationManager= (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, mBuilder.build());
Often you might want to direct the user to the relevant content and so might replace 'new Intent()' with something else.

Categories

Resources