来自通知的意图没有额外内容

Mat*_*ieu 43 android android-intent android-notifications

这似乎是一个常见的问题,我经历了我已经找到的所有相关问题: 活动没有获得新意图,为什么在android通知意图中没有发送额外数据(整数)?,Notification传递旧的Intent Extras,不能为通知中的意图,android挂起意图通知问题添加额外内容 ; 但仍然无法弄清楚这一点.

问题是一样的.我设置了一个带有PendingIntent的通知,其中包含一些额外的信息,而我却没有在另一方面得到它.

以下是生成通知的代码:

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);
Run Code Online (Sandbox Code Playgroud)

另一方面:

boolean start_test=getIntent().getBooleanExtra("start_test", false);
Run Code Online (Sandbox Code Playgroud)

捆绑实际上不存在(getExtras()返回null).

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

如代码所示,我使用getCurrentMillis来确保requestID发生变化....

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

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

Dav*_*ser 48

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

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

  • 但是,如果活动已在任务中运行,则该任务将被带到前台.就这样.意图将不会被传递,onNewIntent()也不会被调用.

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

start_test.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | 
                    Intent.FLAG_ACTIVITY_SINGLE_TOP);
Run Code Online (Sandbox Code Playgroud)

活动是否合适没有区别,你仍然必须在Intent中指定.launchModesingleTopIntent.FLAG_ACTIVITY_SINGLE_TOP

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

请参阅http://code.google.com/p/android/issues/detail?id=17137上有关Google代码的(尚未)未解决的问题

  • 因为它总是将第一个意图传递给它,而不是通知.所以你需要覆盖 - "@Override protected void onNewIntent(Intent intent){super.onNewIntent(intent); setIntent(意向); }所以你的意图将被收到. (3认同)
  • 非常感谢你的详细解释.还必须将PendingIntent.getActivity中的标志设置为FLAG_CANCEL_CURRENT,否则onNewIntent将被多次调用.不完全确定原因,但现在似乎按预期工作. (2认同)

cod*_*emy 39

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

示例代码:

PendingIntent pendIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Run Code Online (Sandbox Code Playgroud)

  • 这是正确的答案.另外请注意,如果活动已经打开,那么onStart不会再次触发,因此需要在onResume中检查intent (2认同)
  • 经过至少2-3个小时的搜索,这节省了我.非常感谢 (2认同)

Udi*_*hef 6

  1. 如果不需要,请避免将清单中的活动设置为单个任务。如果可以,请省略此行或将其更改为 singleTop:

      android:launchMode="singleTask”
    
    Run Code Online (Sandbox Code Playgroud)
  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 );
    
    Run Code Online (Sandbox Code Playgroud)
  3. 如果您只有一个任务 - 通过setIntent(intent)在 onNewIntent() 内重置您的活动中的额外内容

    protected void onNewIntent(Intent intent) {
           setIntent(intent);
           ... 
     }
    
    Run Code Online (Sandbox Code Playgroud)