从通知意图中删除数据 [英] Remove data from notification intent

查看:26
本文介绍了从通知意图中删除数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的启动器活动意图存在问题.场景是:1. 将 Intents 表单通知服务发送到我的启动器活动

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 中,我得到了这个意图.代码是:

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.这工作正常,但是当我来自通知服务时,但是当我从非通知服务启动时,意图中的数据仍然存在.如何从意图中删除该数据.

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.

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

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

1.使用FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS

创建通知时,请在 Intent 中设置标志 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

当用户从最近的任务列表中启动您的应用时,Android 会在传递给 onCreate() 的 Intent 中设置标志 Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY 您的启动活动.您可以在 onCreate() 中检测到此标志的存在,然后您就知道该应用程序是从最近的任务列表而不是通知启动的.在这种情况下,您可以忽略 Intent 中的 extras 仍然包含数据这一事实.

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 作为第四个参数传递给 getActivity().但是,该参数应该是 PendingIntent 标志.如果你想在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);

这篇关于从通知意图中删除数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆