待定意图似乎没有通过附加功能

Pending intent seems to not pass extras

我使用的 Intent 在用户单击推送通知时启动。我在通过未决意图传递额外内容时遇到了一些问题。当我这样做时:

Intent i = new Intent(this, DashboardActivity.class);
i.putExtra("pending", true);
i.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);

PendingIntent pendingIntent = PendingIntent.getActivity(this,0,i,PendingIntent.FLAG_UPDATE_CURRENT);

我期待这种行为:

  1. 待定意图启动 DashboardActivity
  2. 在 DashboardActivity 中我检查额外的东西
  3. 如果我有多余的,去activityB

相反,发生的是:

  1. 待定意图启动 DashboardActivity
  2. 附加信息无效

这是检查 DashboardActivity 中额外内容的代码:

if (getIntent().getExtras() != null) {
    Intent i = new Intent(DashboardActivity.this, B.class);
    startActivity(i);
}

我不知道如何摆脱这种情况,所以如果有人能帮助我,我将不胜感激!

编辑:

private void sendNotification(String messageBody){
    String id="",message="",title="";

    if(type.equals("json")) {
        try {
            JSONObject jsonObject = new JSONObject(messageBody);
            id = jsonObject.getString("id");
            message = jsonObject.getString("message");
            title = jsonObject.getString("title");

        } catch (JSONException e) {
            //            }
        }
    }
    else if(type.equals("message"))
    {
        message = messageBody;
    }

    Intent i = new Intent(this, DashboardActivity.class);
    i.putExtra("pending", true);
    i.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
    i.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

    PendingIntent pendingIntent = PendingIntent.getActivity(this,0,i,PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder notificationBuilder=new NotificationCompat.Builder(this);
    notificationBuilder.setContentTitle(getString(R.string.app_name));
    notificationBuilder.setContentText(message);
    Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    notificationBuilder.setSound(soundUri);
    notificationBuilder.setSmallIcon(R.mipmap.ic_launcher);
    notificationBuilder.setLargeIcon(BitmapFactory.decodeResource(this.getResources(),R.mipmap.ic_launcher));
    notificationBuilder.setAutoCancel(true);
    Vibrator v = (Vibrator) this.getSystemService(Context.VIBRATOR_SERVICE);
    v.vibrate(1000);
    notificationBuilder.setContentIntent(pendingIntent);

    NotificationManager notificationManager=(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(0,notificationBuilder.build());
}

尝试使用PendingIntent.FLAG_UPDATE_CURRENT.

同时添加 Intent.FLAG_ACTIVITY_SINGLE_TOP,这样如果您的 activity 已经打开,就不会重新创建。

将以上 2 个标志添加到您的意图中。

也在你的 DashboardActivity 中实现 onNewIntent

@Override
public void onNewIntent(Intent intent){
    Bundle extras = intent.getExtras();
    if(extras != null){
        if(extras.containsKey("pending"))
     {

     }
  }