Remove data from notification intent
EDIT: I've created a sample application to test this problem and possible solutions. Here are my findings:
If you launch your app from a notification with extras and then later return to your app by selecting it from the list of recent tasks, Android will launch the app again the same way it was launched from the notification (ie: with the extras). This is either a bug or a feature, depending on who you ask.
You'll need to add additional code to deal with this situation. I can offer 2 suggestions:
When you create your notification, set the flag
Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS in the
Intent. Then, when the user selects the notification and launches the app from the notification, this will not create an entry for this task in the list of recent tasks. Also, if there was an entry in the list of recent tasks for this application, that entry will also be removed. In this case, it will not be possible for the user to return to this task from the list of recent tasks. This solves your problem by removing the possibility that the user launches the app from the list of recent tasks (but only when the app has been launched from the notification).
When the user launches your app from the list of recent tasks, Android sets the flag
Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY in the
Intent that is passed to
onCreate() of your launch activity. You can detect the presence of this flag in
onCreate() and then you know that the app has been launched from the recent tasks list and not from the notification. In this case, you can just ignore the fact that the extras in the
Intent still contain data.
Choose the solution that best suits the workflow for your application. And thanks for the question, this was an interesting challenge to solve :-)
You are creating the
PendingIntent incorrectly. You are calling
PendingIntent contentIntent = PendingIntent.getActivity(this,
new Intent(this, MainActivity.class).putExtra("is_log", true),
You are passing
Intent.FLAG_ACTIVITY_CLEAR_TOP as the 4th parameter to
getActivity(). However, that parameter should be
PendingIntent flags. If you want to set
FLAG_ACTIVITY_CLEAR_TOP on the
Intent, you need to do it this way:
PendingIntent contentIntent = PendingIntent.getActivity(this,
new Intent(this, MainActivity.class).putExtra("is_log", true)
How to Use Notification Delete Intent and Save Notification Details If Notification Not Clicked
Where here you can do something like this.
- On notification broadcast store first of all store notification data into you db and put this inserted record id in Pending intent Extras.
- Now while user click on notification at that time you will receive this id in your particular activity from intent extras. get that id from intent extras and delete that recode from your db.
- In case user in not click in notification at that time your notification is there in your db.
If you want to use DeleteIntent() then in that case you need to code like this.
this line will call getDeleteIntent() while user clear notification.
this getDeleteIntent() method will be like this.
protected PendingIntent getDeleteIntent()
Intent intent = new Intent(mContext, NotificationBroadcastReceiver.class);
return PendingIntent.getBroadcast(mContext, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
This method will send one more broadcast with action name "notification_cancelled". you will receive this broadcast and check the action name will be same at that time you will get that deleted intent data.
public void onReceive(Context context, Intent intent)
String action = intent.getAction();
// your code
Now if you want to store notification data then also pass data to the delete broadcast and receive this data from receiver intent and store in your db.
How to disable clean or remove notification on android studio
to use unable to delete notification i have to add new line one notificationCompat.Bilder and it is:
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, channelId)
**If set "setOngoing" notification is true then user can not delete it
Can not remove extras from Activity launched from notification
Problem was in developer settings, "Do not keep activities"
I forget to disable this setting after some specific testing
Maybe someone will find this helpfull :)
I didn't realise this answer would be referred to so much when I first wrote it more then 5 years ago!
I'll clarify to point out that as per @tato-rodrigo answer this won't help you detect an already handled intent in some situations.
Also I should point out I put "clear" in quotes for a reason - you are not really clearing the intent by doing this, you're just using the removal of the extra as a flag that this intent has been seen by the activity already.
I had exactly the same issue.
Above answer put me on the right track and I found even simpler solution, use the:
method call to "clear" the Intent.
Its a bit late answering since this was asked a year ago, but hopefully this helps others in the future.
How to delete an image using Notification pending intent action?
Thanks to @JakeB I am a little bit improved his answer and this is what I got
public class DeleteImageService extends IntentService
private final static String TAG = DeleteImageService.class.getSimpleName();
private static final int DELETE_IMAGE_SERVICE_REQUEST_CODE = 1;
public static final String EXTRA_SCREENSHOT_PATH = "extra_screenshot_path";
public static final String DELETE_IMAGE_SERVICE = "delete_image_service";
public void onCreate()
startService(new Intent(this, DeleteImageService.class));
protected void onHandleIntent(@Nullable Intent intent)
if (intent != null && Intent.ACTION_DELETE.equals(intent.getAction()) && intent.hasExtra(EXTRA_SCREENSHOT_PATH))
String path = intent.getStringExtra(EXTRA_SCREENSHOT_PATH);
Logger.log(Log.ERROR, TAG, path);
public static PendingIntent pendingIntent(@NonNull final Context context,//
@NonNull final String iPath)
final Intent intent = new Intent(context, DeleteImageService.class);
pIntent = PendingIntent.getService(context, DELETE_IMAGE_SERVICE_REQUEST_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT);
This code support android version <26 and also don't request permission for foreground service.
Removing notification after click
Use the flag
Notification notification = new Notification(icon, tickerText, when);
notification.setLatestEventInfo(context, contentTitle, contentText, pendingIntent);
// Cancel the notification after its selected
notification.flags |= Notification.FLAG_AUTO_CANCEL;
and to launch the app:
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
// Create a new intent which will be fired if you click on the notification
Intent intent = new Intent(context, App.class);
// Attach the intent to a pending intent
PendingIntent pendingIntent = PendingIntent.getActivity(context, intent_id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Slowing Speed of Viewpager Controller in Android
How to Get the Height and Width of the Android Navigation Bar Programmatically
Android Fragment Onattach() Deprecated
"Gradle Version 2.10 Is Required." Error
Reference One String from Another String in Strings.Xml
Get Referrer After Installing App from Android Market
Android Popup Window Dismissal
Play Audio File from the Assets Directory
How to Show Dependencies Tree in Android Studio
Custom Filtering in Android Using Arrayadapter
Error:(6, 0) Gradle Dsl Method Not Found: 'Google()'
Using the Android Recognizerintent with a Bluetooth Headset
Android:How to Set Onclick Event for Button in List Item of Listview
Latest Update on Enabling and Disabling Mobile Data Programmatically
How to Access the Drawable Resources by Name in Android