Android多个通知和多个意图

EGH*_*HDK 9 java android android-notifications

我有一个相当简单的应用程序,它接受用户的输入,然后将其设置为通知.用户可以创建他/她喜欢的任意数量的通知.我希望用户单击该通知并进入一个名为的新活动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);
Run Code Online (Sandbox Code Playgroud)
  1. 打开应用程序

  2. 输入"一个"

  3. 点击确定

  4. 通知已发送

  5. 打开应用程序

  6. 输入"Two"

  7. 点击确定

  8. 通知已发送

现在您有两个通知.一个说"一个",一个说"两个".如果您点击通知"两个",它会转到一个显示"两个"的屏幕.完善!

如果单击通知"One",则会转到显示"Two"的屏幕.破碎!

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

    }
Run Code Online (Sandbox Code Playgroud)

小智 15

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

所以你从stackbuilder创建一个pendingIntent,其中包含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;
Run Code Online (Sandbox Code Playgroud)

因此,在您的用例中发生的是您从stackbuilder创建两个相同的pendingintents,第二个intent覆盖第一个intent.实际上你永远不会创建第二个你只是更新第一个的额外内容.

所以不幸的是,你的用例没有可用的标志,但它周围有一个很好的黑客.你可以做的是使用你的resultIntent的setAction并放置一个随机字符串或一个对你的应用有意义的字符串.

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

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


小智 11

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

val pendingIntent = PendingIntent.getActivity(
  this,
  notificationID,
  intent,
  PendingIntent.FLAG_UPDATE_CURRENT
)
Run Code Online (Sandbox Code Playgroud)


Tob*_*zau 8

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

    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);
Run Code Online (Sandbox Code Playgroud)

  • 标题或多或少是id,将"ACTION_"+ System.currentTimeMillis()修复添加到与设置数据相同的意图,并且所有意图都是唯一的.或者甚至更好地添加第二个参数,你可以调用时间到意图(`resultIntent.setData(new Uri.Builder().scheme("data").appendParameter("text","my text").. appendQueryParameter( "time",String.valueOf(System.currentTimeMillis()).build());`) (4认同)