Notif Manager mit dynamischer ID für Notifications?

  • Antworten:4
Phillip
  • Forum-Beiträge: 27

03.09.2014, 21:36:16 via Website

Hallo,

ich bräuchte Hilfe bei der Programmierung von Notifications. In dem unten angegebenen Code habe ich 2 Buttons, die beim Drücken eine Notification auslösen. Das Problem ist, das sie beide eine fixe ID haben (1 und 2) bei

manager.notify(1, notification);

Das Resultat ist, dass egal in welcher Reihnfolge man sie drückt, die Notification1 immer als erstes kommt. Nun haben mir andere Forum-Nutzer geraten diese ID dynamisch zu machen, doch wie geht das? Ich will nur das sie in der Reihnfolge erscheinen in der man sie drückt.
Danke schon mal im Vorraus

MFG Phillip

Antworten
Phillip
  • Forum-Beiträge: 27

03.09.2014, 21:37:39 via Website

Hier der komplette code:

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

}

Antworten
Pascal P.
  • Admin
  • Forum-Beiträge: 11.286

03.09.2014, 21:41:37 via Website

Hättest ruhig bei deinem alten Thread bleiben können.

aber wenn du es dynamisch machst, wie isch geagt habe dann sieht es so aus:

public int id =0; //Id als klassenvar definieren

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(id++, 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(id++, notification);

}

— geändert am 03.09.2014, 21:41:52

LG Pascal //It's not a bug, it's a feature. :) ;)

Phillip

Antworten
Phillip
  • Forum-Beiträge: 27

03.09.2014, 21:55:18 via Website

Danke! Funktioniert!

Antworten
Pascal P.
  • Admin
  • Forum-Beiträge: 11.286

03.09.2014, 21:59:02 via Website

Siehst du so schwierig ist das doch garnicht.
Wärst du da auch selber drauf gekommen?

LG Pascal //It's not a bug, it's a feature. :) ;)

Antworten