2017-12-25 5 views
0

Also habe ich eine App gemacht, wo der Benutzer Benachrichtigungen als Erinnerungen erstellen kann. Die Benachrichtigung wird angezeigt und funktioniert, aber ich möchte die Benachrichtigung löschen, wenn der Benutzer auf eine Aktion klickt. Hier ist mein Code:Wie man die Aktion einer Benachrichtigung einstellt?

  Intent intent = new Intent(); 
      PendingIntent pIntent = PendingIntent.getActivity(MainActivity.this,0,intent,0); 

      Notification notif = new Notification.Builder(MainActivity.this) 
        .setSmallIcon(R.drawable.notification) 
        .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.notification)) 
        .setOngoing(switchState) 
        .setContentTitle("Noteify") 
        .setContentText(notifEditText.getText().toString()) 
        .setPriority(Notification.PRIORITY_MAX) 
        .addAction(R.drawable.delete,"Delete",pIntent) 
        .setContentIntent(pIntent).getNotification(); 


      NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); 
      nm.notify(0,notif); 

Ich habe die Aktion, aber ich weiß nicht, wie man tatsächlich die Meldung löschen.

Antwort

0

Einfach, einfach diese nennen:

mBuilder.setAutoCancel(true); 

Unterhalb dieser

NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(context) 
    .setContentTitle("MyApp") 
    .setContentText(message) 
    .setDefaults(Notification.DEFAULT_ALL) 
    .setAutoCancel(true) 
    .setContentIntent(contentIntent) 
    .setSmallIcon(icon); 
+0

testen Wird mich –

+0

lassen weiß, ich habe genau das, und es funktioniert nicht. – kayz

0

Gowthaman M sagte richtig ist, hinzufügen setAutoCancel(true); zu Notification Objekt.

bearbeiten Sie Ihren Code wie unten,

 Intent intent = new Intent(); 
     PendingIntent pIntent = PendingIntent.getActivity(MainActivity.this,0,intent,0); 

     Notification notif = new Notification.Builder(MainActivity.this) 
      .setSmallIcon(R.drawable.notification) 
      .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.notification)) 
      .setOngoing(switchState) 
      .setContentTitle("Noteify") 
      .setAutoCancel(true) 
      .setContentText(notifEditText.getText().toString()) 
      .setPriority(Notification.PRIORITY_MAX) 
      .addAction(R.drawable.delete, "Delete", pIntent) 
      .setContentIntent(pIntent).getNotification(); 


     NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); 
     nm.notify(0,notif); 
Verwandte Themen