Android 通知的意图没有额外内容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11551195/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 08:00:44  来源:igfitidea点击:

Intent from notification does not have extras

androidandroid-intentandroid-notifications

提问by Matthieu

This seem to be a common problem and I went through all the related questions I could find already: Activity isn't picking up new intent, Why extra data (integer) is not sent in android notification intent?, Notification passes old Intent Extras, Can't put extras for an intent in notification, android pending intent notification problem; but still cannot figure this out.

这似乎是一个常见问题,我已经解决了我已经找到的所有相关问题: Activity 没有选择新的意图为什么在 android 通知意图中没有发送额外的数据(整数)?,通知通过旧的 Intent Extras不能在通知中放置一个意图的附加项android 待处理的意图通知问题;但仍然无法弄清楚这一点。

Problem is the same. I set a notification with a PendingIntent carrying some extra information and I don't get it on the other side.

问题是一样的。我用 PendingIntent 设置了一个带有一些额外信息的通知,但我没有在另一边得到它。

Here is the code for generating the notification:

这是生成通知的代码:

Notification notification = new Notification(R.drawable.icon, getResources().getString(R.string.notification_ticker), System.currentTimeMillis());
notification.flags = Notification.FLAG_AUTO_CANCEL | Notification.FLAG_ONLY_ALERT_ONCE;

Intent start_test = new Intent(this, MyActivity.class);
start_test.putExtra("start_test", true);
start_test.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

PendingIntent pi = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), start_test, PendingIntent.FLAG_CANCEL_CURRENT);
notification.setLatestEventInfo(this, getResources().getString(R.string.notification_title), getResources().getString(R.string.notification_content, expired), pi);
nm.notify(NOTIFICATION_ID, notification);

And on the other side:

另一方面:

boolean start_test=getIntent().getBooleanExtra("start_test", false);

The bundle is actually not there (getExtras() returns null).

该包实际上不存在(getExtras() 返回 null)。

I tried the different flags for PendingIntent.getActivity(FLAG_UPDATE_CURRENT, FLAG_CANCEL_CURRENT, FLAG_ONE_SHOT), none of those helped.

我为PendingIntent.getActivity( FLAG_UPDATE_CURRENT, FLAG_CANCEL_CURRENT, FLAG_ONE_SHOT)尝试了不同的标志,但这些都没有帮助。

As shown in the code, I use getCurrentMillis to make sure the requestID changes....

如代码所示,我使用 getCurrentMillis 来确保 requestID 更改....

Also found that in MyActivity, onCreateand onNewIntentare not getting called. only onResumeis. Even though the FLAG_ACTIVITY_NEW_TASKis set... ?

还发现在 MyActivity 中,onCreateonNewIntent没有被调用。只是onResume。即使FLAG_ACTIVITY_NEW_TASK设置了...?

Am I missing something very simple ?

我错过了一些非常简单的东西吗?

回答by David Wasser

Setting FLAG_ACTIVITY_NEW_TASKfor the notification Intent will cause the following:

FLAG_ACTIVITY_NEW_TASK通知 Intent 的设置将导致以下情况:

  • If the activity is notalready running in a task, a new task will be started and the Intent will be delivered to the activity in onCreate()

  • However, if the activity is already running in a task, that task will be brought to the foreground. That's all. The Intent will notbe delivered and onNewIntent()will not be called.

  • 如果该活动是不是已经在运行的任务,新的任务将被启动,并且打算将传递到活动onCreate()

  • 但是,如果 Activity 已经在某个任务中运行,则该任务将被置于前台。就这样。Intent不会被传递,onNewIntent()也不会被调用。

If you want the Intent to actually be deliveredyou need to specify:

如果您希望实际交付Intent,则需要指定:

start_test.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | 
                    Intent.FLAG_ACTIVITY_SINGLE_TOP);

It makes no differencewhether the launchModeof the activity is singleTopor not, you still must specify Intent.FLAG_ACTIVITY_SINGLE_TOPin the Intent.

它使没有差别是否launchMode该活动的是singleTop或不是,你仍然必须指定Intent.FLAG_ACTIVITY_SINGLE_TOP的意图。

Note:If the activity is already running in a task and the activity is noton top of the activity stack in that task, the task will be brought to the foreground. That's all. The Intent will notbe delivered. The only way to make this happen would be to add Intent.FLAG_ACTIVITY_CLEAR_TOPto the other 2 flags, but this may not be what you want to happen (depends on your specific scenario).

注意:如果 Activity 已经在任务中运行,并且该 Activity不在该任务中的 Activity 堆栈顶部,则该任务将被置于前台。就这样。将不会交付意图。实现这一点的唯一方法是添加Intent.FLAG_ACTIVITY_CLEAR_TOP到其他 2 个标志,但这可能不是您想要发生的(取决于您的具体情况)。

See my (still) open issue on Google code at http://code.google.com/p/android/issues/detail?id=17137

http://code.google.com/p/android/issues/detail?id=17137上查看我关于 Google 代码的(仍然)未解决的问题

回答by codingjeremy

I just added the PendingIntent.FLAG_UPDATE_CURRENTflag to my pending intent, and it started working for me (skipped all flags for Intent).

我刚刚将PendingIntent.FLAG_UPDATE_CURRENT标志添加到我的待处理意图中,它开始为我工作(跳过意图的所有标志)。

Example Code:

示例代码:

PendingIntent pendIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

回答by Udi Reshef

  1. If not needed then avoidsetting your activity in manifest as a single task. omit this line or change it to singleTop if you can:

      android:launchMode="singleTask”
    
  2. If you must have a single task then in pending intent set flags to : Intent.FLAG_ACTIVITY_NEW_TASK| Intent.FLAG_ACTIVITY_CLEAR_TASKAnd PendingIntent.FLAG_CANCEL_CURRENT.

    resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |Intent.FLAG_ACTIVITY_CLEAR_TASK);
    pIntent = PendingIntent.getActivity(context, 0, resultIntent,    PendingIntent.FLAG_CANCEL_CURRENT );
    
  3. If you have a single task - Reset your extras in your activity inside onNewIntent() by setIntent(intent);

    protected void onNewIntent(Intent intent) {
           setIntent(intent);
           ... 
     }
    
  1. 如果不需要,请避免将清单中的活动设置为单个任务。如果可以,请省略此行或将其更改为 singleTop:

      android:launchMode="singleTask”
    
  2. 如果您必须有一个任务,那么在待处理的意图中将标志设置为:意图。FLAG_ACTIVITY_NEW_TASK| 意图。FLAG_ACTIVITY_CLEAR_TASK和 PendingIntent。FLAG_CANCEL_CURRENT

    resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |Intent.FLAG_ACTIVITY_CLEAR_TASK);
    pIntent = PendingIntent.getActivity(context, 0, resultIntent,    PendingIntent.FLAG_CANCEL_CURRENT );
    
  3. 如果您只有一个任务 - 通过setIntent(intent)在 onNewIntent() 内重置您的活动中的额外内容

    protected void onNewIntent(Intent intent) {
           setIntent(intent);
           ... 
     }
    

回答by mohit

You can send extras in intent following way

您可以按照以下方式有意发送额外内容

PendingIntent contentIntent ;
Intent intent = new Intent(this,TestActivity.class);
intent.putExtra("extra","Test");
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);

stackBuilder.addParentStack(ArticleDetailedActivity.class);

contentIntent = stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);

To Get Intent extra value in Test Activity class you need to write following code :

要在测试活动类中获得 Intent 额外价值,您需要编写以下代码:

Intent intent = getIntent();
 String extra = intent.getStringExtra("extra") ;

回答by Panayiotis Georgiou

I have used something like this

我用过这样的东西

Intent myIntent = new Intent(context, DoSomething.class);
 PendingIntent pendingIntent = PendingIntent.getActivity(
        context, 
        0, 
        myIntent, 
        Intent.FLAG_ACTIVITY_NEW_TASK);

Check out the full example here

在此处查看完整示例

回答by user1676075

Simpler approach: the real problem is that the Intent, being to the same target, gets replaced. I've solved this by just created a new service, a "NotificationDismissalService". The only intent going to that service is the setDeleteIntent item. Because it's a unique service, the parameters do not get replaced. The body of NotificationDismissalService (and it would really be one such dismissal service per unique intent type) is a simple implementation of "onStartCommand" that sends an intent to the preferred service (reading/writing the correct parameters). Because that's an actual send of a service, without a pendingintent in between, it works just fine.

更简单的方法:真正的问题是,同一个目标的 Intent 被替换了。我刚刚通过创建一个新服务“NotificationDismissalService”解决了这个问题。进入该服务的唯一意图是 setDeleteIntent 项。因为它是一个独特的服务,所以参数不会被替换。NotificationDismissalService 的主体(对于每个独特的意图类型,它实际上是一个这样的解除服务)是“onStartCommand”的一个简单实现,它将意图发送到首选服务(读取/写入正确的参数)。因为这是一个服务的实际发送,中间没有挂起的意图,所以它工作得很好。