如何在Notify管理器(Android)上制作动态ID?

时间:2014-09-03 19:35:02

标签: android dynamic notifications

我的Android应用程序出现问题。我有两个按钮,两个按钮都会在按下后发出通知,但这里是问题所在。两者都有一个不变的ID(1和2)

manager.notify(1, notification);

这意味着notification1始终显示在状态栏中的第一个。当你先按通知2时它没有任何区别,它始终是notification1然后是notification2。现在我需要知道如何制作动态ID

有人能告诉我如何避免不变的命令吗?

这里有完整的代码:

 public void sendNotification(View view) {

    switch(view.getId()){

        case R.id.button1:
            Notification1();
            break;

        case R.id.button2:
            Notification2();
            break;

        }
     }

private void Notification1() {

   NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
   builder.setAutoCancel(true);
   builder.setContentTitle("BasicNotification");
   builder.setContentText("Test");
   builder.setSmallIcon(R.drawable.icon1);

   Notification notification = builder.build();
   NotificationManager manager = (NotificationManager) this.getSystemService(NOTIFICATION_SERVICE);
   manager.notify(1, notification);

}

private void Notification2() {

   NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
   builder.setAutoCancel(true);
   builder.setContentTitle("BasicNotification");
   builder.setContentText("Test");
   builder.setSmallIcon(R.drawable.icon2);

   Notification notification = builder.build();
   NotificationManager manager = (NotificationManager) this.getSystemService(NOTIFICATION_SERVICE);
   manager.notify(2, notification);

}

2 个答案:

答案 0 :(得分:2)

System.currentTimeInMillis()是我生成唯一ID的首选方式。

manager.notify(System.currentTimeInMillis(), notification);

答案 1 :(得分:0)

//generating unique ID
int uniqueID = (int) System.currentTimeMillis();

现在你可以在你的代码中使用它了

manager.notify(uniqueID , notification);
相关问题