拆除通知意图数据意图、通知、数据

2023-09-12 01:21:09 作者:做人实称点

我有问题,我发射activity.Scenerio的意图是: 1.发送意图形成通知服务,以我的发射活动。

I have issue in intent of my launcher activity.Scenerio is: 1. Send intents form notification service to my launcher activity

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

2。在我MainActivity我得到这个意图。 code是:

2. In my MainActivity i getting this intent. code is:

if(this.getIntent().getExtras()!=null){

        boolean isLogNewTripScreen  = (boolean)this.getIntent().getExtras().getBoolean("is_log");

    }
    }

3。这项工作很好,但是当我来​​自通知服务,但是当我从发动不通知服务,在intentis数据仍然there.How我可以删除的意图数据。

3. this work fine but when i come from notification service,but when i launch from not notification service ,that data in intentis still there.How can i remove that data from intent.

推荐答案

编辑:我创建了一个示例应用程序来测试这个问题,可能的解决方案。这里是我的发现:

如果您有额外的通知启动您的应用程序,然后再从近期的任务列表中选择返回到您的应用程序,Android将再次启动应用程序,它从通知(即推出了同样的方法:用群众演员)。这可以是一个错误或功能,这取决于谁你问。

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.

您将需要添加额外的code来处理这种情况。我可以提供2个建议:

You'll need to add additional code to deal with this situation. I can offer 2 suggestions:

1。使用 FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS

1. Use FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS

在创建您的通知,在意图设置标志 Intent.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。检测 FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY

2. Detect FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY

当用户从近期的任务列表启动您的应用程序,Android的设置在意图传递给的onCreate()您推出的活动。你可以发现这个标志的presence在的onCreate()键,那么你就知道该应用程序已经启动,从近期的任务列表和不从通知。在这种情况下,你可以忽视的事实是,在演员的意图仍包含数据。

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

信息:

您正在创建的 PendingIntent 不正确。您调用

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

正在通过 Intent.FLAG_ACTIVITY_CLEAR_TOP 作为第4个参数 getActivity()。然而,参数应该是 PendingIntent 标志。如果你想设置 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);