16

我有一个相当简单的应用程序,它接受用户的输入,然后将其设置为通知。用户可以创建任意数量的通知,只要他/她喜欢。我希望用户单击通知并进入一个名为ResultActivity. ResultActivity依次putExtras从通知意图中读取并将其显示给用户。下面的代码允许我做几乎所有我想做的事情,除了任何时候按下通知,我都会收到putExtra最后创建的通知。

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);

    }
4

4 回答 4

25

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

因此,您从带有 update_current 标志的 stackbuilder 创建了一个pendingIntent。如果您查看 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;

因此,在您的用例中发生的情况是,您从 stackbuilder 创建了两个相同的 pendingintent,第二个意图覆盖了第一个。实际上,您永远不会创建第二个,您只需更新第一个的附加内容。

所以不幸的是,你的用例没有可用的标志,但有一个很好的技巧。您可以做的是使用 resultIntent 的 setAction 并放置一个随机字符串或对您的应用有意义的字符串。

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

这将使您的 resultIntent 足够独特,以便 pendingIntent 将创建它而不是更新以前的。

于 2015-05-01T03:49:34.710 回答
13

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

val pendingIntent = PendingIntent.getActivity(
  this,
  notificationID,
  intent,
  PendingIntent.FLAG_UPDATE_CURRENT
)
于 2019-05-21T14:24:32.627 回答
9

您创建了多个混合的意图。我清理了代码(但没有测试它)

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

    Resources res = ctx.getResources();

    // Creates an explicit intent for an Activity in your app
    Intent resultIntent = new Intent(this, ResultActivity.class);
    String pass = text.getText().toString();
    resultIntent.setData(new Uri.Builder().scheme("data")
            .appendQueryParameter("text", "my text").build());
    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);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(ctx);
    builder.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())
            .setContentIntent(resultPendingIntent);

    Notification n = builder.build();
    n.flags = Notification.FLAG_NO_CLEAR;
    nm.notify(i++, n);

    text.setText(null);
于 2012-10-19T06:14:31.097 回答
0

使用一些随机 requestCode 来分隔两个通知

PendingIntent pendingIntent = PendingIntent.getActivity(context, CommonTools.getRandomNumber(1, 100),
            notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

public int getRandomNumber(int min, int max) {
    // min (inclusive) and max (exclusive)
    Random r = new Random();
    return r.nextInt(max - min) + min;
}
于 2019-07-18T07:51:42.393 回答