Lösche einen bestimmten Alarm / bestimmte Notification

  • Antworten:0
AndroidEntwickler
  • Forum-Beiträge: 58

14.12.2015, 07:48:40 via Website

Guten Morgen zusammen,

ich brauch eure Hilfe.
Bin neu in der Android Entwickler Welt und arbeite mich Stück für Stück vor und lerne dabei so viel es geht.
Allerdings komm ich jetzt aktuell nicht weiter.

Ich kann eine Notification mit Hilfe des Alarm Managers erstellen, um diese zeitverzögert anzeigen zu lassen.
Allerdings würde ich jetzt gerne wissen, wie ich eine notification oder einen Alarm (je nach dem was jetzt zutrifft) ändern bzw. löschen kann?

Ich habe z.B. die Notification ID in einer Variable gespeichert und nun möchte ich den Alarm / die Notification mit der jeweiligen ID löschen / ändern (z.B. die Zeit wann die Notification erscheinen soll ändern)

Für das Erstellen habe ich folgenden Code:

Main Activity

scheduleNotification(getNotification("Das ist mein Notification Text"), 1000);

// NOTIFICATIONS
    private void scheduleNotification(Notification notification, int delay) {
        Intent notificationIntent = new Intent(this, NotificationPublisher.class);
        notificationIntent.putExtra("NOTIFICATION_ID", (int)System.currentTimeMillis()*(-1));
        notificationIntent.putExtra("NOTIFICATION", notification);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this, (int)System.currentTimeMillis()*(-1), notificationIntent, PendingIntent.FLAG_ONE_SHOT);

        long futureInMillis = System.currentTimeMillis() + delay;
        AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
        alarmManager.set(AlarmManager.RTC_WAKEUP, futureInMillis, pendingIntent);
    }

    private Notification getNotification(String content) {
        Notification.Builder builder = new Notification.Builder(this);
        builder.setContentTitle(getString(R.string.app_name));
        builder.setContentText(content);
        builder.setSmallIcon(R.drawable.ic_notification_appicon);
        return builder.getNotification();
    }

NotificationPublisher

public class NotificationPublisher extends BroadcastReceiver {

    public void onReceive(Context context, Intent intent) {

        NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notification = intent.getParcelableExtra("NOTIFICATION");
        notificationManager.notify(intent.getIntExtra("NOTIFICATION_ID", 0), notification);

    }

}

Antworten