安卓 多个通知,具有多种意图

2022-09-02 23:09:37

我有一个相当简单的应用程序,它从用户那里获取输入,然后将其设置为通知。用户可以创建任意数量的通知。我希望用户单击通知并转到名为 的新活动。 反过来,从通知意图中读取并显示给用户。下面的代码允许我做我想做的几乎所有事情,除非每当按下通知时,我都会收到上次创建的通知。ResultActivityResultActivityputExtrasputExtra

Intent notificationIntent = new Intent(ctx, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(ctx, i,notificationIntent,PendingIntent.FLAG_CANCEL_CURRENT);

NotificationManager nm = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);

Resources res = ctx.getResources();
NotificationCompat.Builder builder = new NotificationCompat.Builder(ctx);
builder.setContentIntent(contentIntent)
    .setSmallIcon(R.drawable.ic_launcher)
    .setLargeIcon(BitmapFactory.decodeResource(res,R.drawable.ic_launcher))
    .setTicker("Remember to " + text.getText())
    .setWhen(System.currentTimeMillis()).setAutoCancel(true)
    .setContentTitle(text.getText());

// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(this, ResultActivity.class);
String pass = text.getText().toString();

resultIntent.putExtra("title", pass);
resultIntent.putExtra("uid", i);

TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(ResultActivity.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);

new Uri.Builder().scheme("data").appendQueryParameter("text", "my text").build();
builder.setContentIntent(resultPendingIntent);

Notification n = builder.build();
n.flags = Notification.FLAG_NO_CLEAR;
nm.notify(i++, n);
text.setText(null);
  1. 打开应用程序

  2. 输入“一”

  3. 点击确定

  4. 通知已发送

  5. 打开应用程序

  6. 输入“两个”

  7. 点击确定

  8. 通知已发送

现在你有两个通知。一个说“一”,一个说“二”。如果您单击通知“两个”,它会将您带到一个显示“两个”的屏幕。完善!

如果您单击通知“一”,它会将您带到一个显示“两个”的屏幕。破碎!

结果活动.java

public class ResultActivity extends Activity {
    String title = null;
    TextView text;

    int i=0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity);
        text = (TextView) findViewById(R.id.textView1);



        title = getIntent().getStringExtra("title");
         i = getIntent().getIntExtra("uid", 0);


        text.setText(title);

    }

答案 1

我知道这是很久以前的事了,但我觉得答案并没有说明代码中的问题。所以问题几乎就在这里PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);

因此,您可以从堆栈构建器创建一个挂起的Intent,并带有update_current的标志。如果你看一下FLAG_UPDATE_CURRENT它说

 /**
 * Flag indicating that if the described PendingIntent already exists,
 * then keep it but replace its extra data with what is in this new
 * Intent. For use with {@link #getActivity}, {@link #getBroadcast}, and
 * {@link #getService}. <p>This can be used if you are creating intents where only the
 * extras change, and don't care that any entities that received your
 * previous PendingIntent will be able to launch it with your new
 * extras even if they are not explicitly given to it.
 */
public static final int FLAG_UPDATE_CURRENT = 1<<27;

因此,在您的用例中发生的情况是,您从堆栈构建器创建两个相同的挂起,并且第二个意图覆盖第一个目的。实际上,您永远不会创建第二个,而只是更新第一个的附加功能。

因此,不幸的是,您的用例没有可用的标志,但是有一个很好的技巧可以解决它。你可以执行的操作是使用 resultIntent 的 setAction,并放置一个随机字符串或对你的应用有意义的字符串。

例如。resultIntent.setAction("dummy_action_" + notification.id);

这将使您的结果Intent足够独特,以便待定Intent将创建它而不是更新以前的结果。


答案 2

设置不同有助于我创建和更新当前意图。requestCode

val pendingIntent = PendingIntent.getActivity(
  this,
  notificationID,
  intent,
  PendingIntent.FLAG_UPDATE_CURRENT
)

推荐