Remove Data from Notification Intent

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:

1. Use FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS

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).

2. Detect FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY

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 :-)


Additional information:

You are creating the PendingIntent incorrectly. You are calling

PendingIntent contentIntent = PendingIntent.getActivity(this,
TripLoggerConstants.PENDING_TRIPS_NOTIFICATION_ID,
new Intent(this, MainActivity.class).putExtra("is_log", true),
Intent.FLAG_ACTIVITY_CLEAR_TOP);

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,
TripLoggerConstants.PENDING_TRIPS_NOTIFICATION_ID,
new Intent(this, MainActivity.class).putExtra("is_log", true)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP), 0);

How to Use Notification Delete Intent and Save Notification Details If Notification Not Clicked

Where here you can do something like this.

  1. On notification broadcast store first of all store notification data into you db and put this inserted record id in Pending intent Extras.
  2. 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.
  3. 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.

notificationBuilder.setDeleteIntent(getDeleteIntent());

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);
intent.setAction("notification_cancelled");
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.

@Override
public void onReceive(Context context, Intent intent)
{
String action = intent.getAction();
if(action.equals("notification_cancelled"))
{
// 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) 
.setSmallIcon(R.mipmap.ic_push_notificatioin)
.setOngoing(true)

**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 :)

Clearing intent


UPDATE:

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:

getIntent().removeExtra("key"); 

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 DeleteImageService()
{
super(DELETE_IMAGE_SERVICE);
}

@Override
public void onCreate()
{
super.onCreate();
startService(new Intent(this, DeleteImageService.class));
}

@Override
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);
FileManager.deleteFileBy(path);
Logger.log(Log.ERROR, TAG, path);
PushNotificationManager.getInstance(this).getScreenshotNotificator(this).closeNotification();
}
}

@NonNull
public static PendingIntent pendingIntent(@NonNull final Context context,//
@NonNull final String iPath)
{
final Intent intent = new Intent(context, DeleteImageService.class);
intent.setAction(Intent.ACTION_DELETE);
intent.putExtra(EXTRA_SCREENSHOT_PATH, iPath);
PendingIntent pIntent;
pIntent = PendingIntent.getService(context, DELETE_IMAGE_SERVICE_REQUEST_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT);

return pIntent;
}
}

This code support android version <26 and also don't request permission for foreground service.

Removing notification after click

Use the flag Notification.FLAG_AUTO_CANCEL

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);


Related Topics



Leave a reply



Submit