挂起的意图似乎无法传递额外信息

3

我使用一个Intent来在用户点击推送通知时启动。我遇到了一些在pending 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. 如果有额外信息,跳转到B活动页面

然而,实际发生的是:

  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());
}
1个回答

1
尝试使用 PendingIntent.FLAG_UPDATE_CURRENT
另外添加 Intent.FLAG_ACTIVITY_SINGLE_TOP,以便如果您的活动已经打开,则不会重新创建。
将上述2个标志添加到您的意图中。
还要在您的DashboardActivity中实现 onNewIntent
@Override
public void onNewIntent(Intent intent){
    Bundle extras = intent.getExtras();
    if(extras != null){
        if(extras.containsKey("pending"))
     {

     }
  }

嗨kapsym,PendingIntent.FLAG_UPDATE_CURRENT已经插入到pendingIntent中,在Intent.FLAG_ACTIVITY_SINGLE_TOP之后没有任何变化。 - BigMeister
刚刚注意到,你把启动待定意图的代码放在哪里了?它是你的通知管理器的一部分吗?我的意思是,在触发通知时你这样做了吗? - Kapil G
是的,Yes 是 Notification Manager 的一部分。在 pending intent 之后,有一个 notificationbuilder,它具有 setContentIntent() 属性。 - BigMeister
我已经添加了代码,当有推送通知进来时会触发。 - BigMeister
已经添加了更新。你实现了onNewIntent方法还是在onCreate中尝试获取意图? - Kapil G
显示剩余3条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接