android-总是从最近的通知中获取数据

时间:2019-03-03 06:13:21

标签: android android-notifications

这是我发出通知的代码,问题是,当我有多个通知并单击其中一个时,它总是从上次通知中返回意图数据,这是我的代码;

Intent resultIntent = resultIntent = new Intent(mContext, intentClass);
            resultIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            resultIntent.putExtra("onvan", title);
            resultIntent.putExtra("message", message);
            resultIntent.putExtra("link", link);
            resultIntent.putExtra("linktype", linktype);
            resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                    | Intent.FLAG_ACTIVITY_SINGLE_TOP);

            TaskStackBuilder TSB = TaskStackBuilder.create(mContext);
            TSB.addParentStack(intentClass);
            TSB.addNextIntent(resultIntent);
            PendingIntent resultPendingIntent =
                    TSB.getPendingIntent(
                            new Random().nextInt(),
                            PendingIntent.FLAG_UPDATE_CURRENT
                    );

            mBuilder = new NotificationCompat.Builder(mContext, NOTIFICATION_CHANNEL_ID);
            mBuilder.setSmallIcon(R.mipmap.ic_launcher);

            mBuilder.setContentIntent(resultPendingIntent);
            mBuilder.setContentTitle(title)
                    .setContentText(message)
                    .setAutoCancel(true)
                    .setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
                    .setContentIntent(resultPendingIntent);

在我的活动中,我使用以下代码从通知中获取意图:

final Intent intent = getIntent();
    if (intent != null) {
        if (intent.getExtras() != null && intent.getExtras().getString("message") != null && intent.getExtras().getString("link").length() <= 1) {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setMessage(intent.getExtras().getString("message"))
                    .setCancelable(false)
                    .setPositiveButton(("بستن"), new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            dialog.cancel();
                        }
                    });
            AlertDialog alert = builder.create();
            alert.show();
            TextView messageView = (TextView) alert.findViewById(android.R.id.message);
            messageView.setGravity(Gravity.RIGHT);
            messageView.setTypeface(typeface);
        }
    }

我想从我单击的通知中获取数据,而不是最后一个。

我该如何解决?

0 个答案:

没有答案