Autocanel von Notifications bei zweifachen Aufrufen?

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

05.09.2014, 11:19:09 via Website

Hallo,

ich habe folgendes Problem. Ich habe 2 Buttons die bei Verwendung eine Notitfication auslösen. Das funktioniert ganz gut. Das Problem ist jedoch, wenn ich einen von diesen Buttons mehrfach drücke, dann ist die komplett gleiche Benachrichtung mehrfach im Status-bar. Wie kann ich das verhindern? Ich hätte mir gedacht das wenn man es zwei mal drückt, dass dann automatisch die erste Benachrichtigung gelöscht wird und nur noch die zweite da ist. Wüsste da jemand was?

Danke schon mal in Vorraus.

public class MainActivity extends ActionBarActivity {

@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); }

@Override public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;

}

public void sendNotification(View view) {

switch(view.getId()){

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

    case R.id.buttom2:
        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((int)
System.currentTimeMillis(), 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((int)
System.currentTimeMillis(), notification);

}

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

05.09.2014, 11:38:55 via Website

Du übergibst dem Notification Manager immer die aktuelle Zeit.
Wenn dann musst du die die Zeit speichern und erst die alte Notification löschen, bevor du di neue erstelltst.

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

Antworten
Jakob N.
  • Forum-Beiträge: 282

05.09.2014, 11:52:39 via App

Man kann ein bestimmtes Flag setzen, weiß jetzt leider nicht auswendig welches.

LG Jakob

Antworten
Fabian Simon
  • Forum-Beiträge: 359

05.09.2014, 12:53:19 via Website

Du kannst auch mal versuchen, das builder object zu behalten (also global zu definieren).
und nur noch den Text zu ändern und es neu zu builden.
Da es sich ja um eine reverenz handeln müsste, solltest du ja auch auf das referenzeobjekt weiterhin zugreifen können

Antworten
Phillip
  • Forum-Beiträge: 27

05.09.2014, 13:11:18 via Website

okay ich bin jetzt doch wieder auf deine Methode umgestiegen (also nicht mehr das mit System.currentTimeMillis(), notification)), wüsstest du dann jetzt was einfacheres? :)

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

05.09.2014, 13:42:32 via Website

Jup.

HashMap<String,int> map = new HashMap<String,int>();

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); 
  namager.cancel((int)map.get("notif1")); //Vlt noch pr&uuml;fen obder Wert in de Hash Map exisitert, k&ouml;nne ne nullPointer exception auftreten

manager.notify(id++, notification);
map.put("notif1",id);

}

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.cancel((int)map.get("notif2")); //Vlt noch pr&uuml;fen obder Wert in de Hash Map exisitert, k&ouml;nne ne nullPointer exception auftreten
manager.notify(id++, notification);
map.put("notif2",id);

}

Also jetzt hast du eine Globale Liste die HashMap mit einem String als Key und einem Integer.
Wenn du eine Notification erstellst, dann wird die ID in dierser Liste gespeichert,.
Wenn du jetzt neu auf den Button drückst, muss die Alte notification erst gelöscht werden, un eine doppelung zu verhindern.
Deswegen holt man sich die alte notification ID und lösst diese löschen.
Danach kann man per notify eine neue erzeugen.

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

Antworten