Android的多个通知,并与多个意向(Android Multiple Notifications

2019-08-01 15:25发布

我有一个非常简单的应用程序,需要从用户的输入,然后将其设置为一个通知。 因为他/她喜欢的用户可以创建任意多个通知。 我希望用户单击该通知并获得带到一个所谓的新活动ResultActivityResultActivity又在读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. 发送通知

现在你有两个通知。 一,上面写着“一”,另一个说“二”。 如果你点击通知“两个”需要你,说“二”的画面。 完善!

如果你点击通知“一”,就需要你,说“二”的画面。 破碎!

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

    }

Answer 1:

您可以创建多个意图被混合。 我清理代码(但没有测试)

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


Answer 2:

我知道这是一个很多时间前,但我觉得这个答案没有说你的代码中的任何问题。 所以,问题是相当多这里PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);

所以你创建一个从stackbuilder蒙山update_current国旗的的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两个相同pendingintents和第二意图覆盖的第一个。 其实你从来没有创建第二个你刚刚更新的第一个的临时演员。

所以,不幸的是你的使用情况没有可用的标志,但它周围有一个不错的黑客。 你可以做的是使用你的resultIntent的setAction命令并放置一个随机字符串或有意义的您的应用程序的字符串。

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

这会让你的resultIntent足够独特,使的PendingIntent将创建它,而不是更新前一个。



Answer 3:

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

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


Answer 4:

用一些随机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;
}


文章来源: Android Multiple Notifications and with multiple intents