Android:我的部分内容在未决意图中迷失了

bia*_*ova 5 android extras android-intent android-fragments android-pendingintent

我是第一次制作Android应用程序,我遇到了这个问题.当用户A向另一个用户B发送好友请求时,用户B获得通知.我希望当用户B点击通知时重定向到用户A配置文件.

主要活动(应用程序中的主页)根据用户来自何处打开不同的片段.用户简档就是这样一个片段.

这就是我从主活动的演示者类发送通知的方式.

Intent notificationIntent = new Intent(activity.getApplicationContext(), MainActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

Bundle extras = new Bundle();
extras.putString("name" ,this.friendRequestFromUserName.toString());
extras.putString("email", this.friendRequestFromUserEmail);
extras.putString("notification", "notificationFriendRequest");

notificationIntent.putExtras(extras);

System.out.println("PUTTING EXTRA");
System.out.println(notificationIntent.getExtras().toString());
// output here is:
//Bundle[{name=The Lion King, email=mufasa@lionking.com, notification=notificationFriendRequest}]

NotificationCompat.Builder builder = new NotificationCompat.Builder(activity)
    .setSmallIcon(R.drawable.ic_notification)
    .setContentTitle("New friend!")
    .setContentText(this.friendRequestFromUserName + " wants to be friends.")
    .setAutoCancel(true)
    .setOnlyAlertOnce(true)
    .setOngoing(false)
    .setContentIntent(PendingIntent.getActivity(activity.getApplicationContext(), 0, notificationIntent, 0));

builder.setOngoing(false);
builder.setAutoCancel(true);

Notification not = builder.build();
not.flags = Notification.FLAG_AUTO_CANCEL;
NotificationManager manager = (NotificationManager)this.activity.getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
        manager.notify(123, not);
Run Code Online (Sandbox Code Playgroud)

但是在主要活动中,当用户点击通知时,只有部分Bundle是"到达".以下是我尝试获取额外内容的方法:

    extraName = intent.getStringExtra("name");
    extraMail = intent.getStringExtra("email");
    extra = intent.getStringExtra("notification");
Run Code Online (Sandbox Code Playgroud)

    System.out.println(intent.getExtras().toString());
    //Bundle[{notification=notificationFriendRequest}]
Run Code Online (Sandbox Code Playgroud)

我也尝试将数组作为额外的结果使用相同的结果.由于我的额外部分是抵达,但其他部分没有,我找不到类似的主题和问题,我决定询问是否有人可以帮助或解释为什么或如何发生这种情况.谢谢.

bia*_*ova 10

因此,正如pskink建议我添加了唯一的requestCode和PendingIntent.FLAG_UPDATE_CURRENT标志并解决了它.

.setContentIntent(PendingIntent.getActivity(activity.getApplicationContext(), Math.abs(generator.nextInt()), notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT));
Run Code Online (Sandbox Code Playgroud)

由于我没有看到如何将建议标记为答案,因为这是我的第一个问题,我自己也在回答这个问题.谢谢你,pskink!

  • Android,总是给我带来惊喜。 (4认同)