Über Notification Methode aufrufen

  • 9 Antworten
  • Letztes Antwortdatum
B

Benützername_re_54

Neues Mitglied
0
Hallo liebe Leute.
Und zwar bin ich grade dabei meine erste App zu programmieren.
Ich habe meiner App eine "Notification" hinzugefügt:

Code:
Notification.Builder builder = new Notification.Builder(this)
                .setTicker(".......")
                .setSmallIcon(R.drawable.ic_launcher)
                .setContentTitle(......)
                .setContentText(".........")
                .setAutoCancel(false);
        nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        nm.notify("interstitial_tag", 0, builder.getNotification());
Wenn ich auf die Benachrichtigung klicke, möchte ich dass eine Methode aufgerufen wird.
Könnt ihr mir bitte sagen wie das funktioniert mit eventuell einem Coding?
 
Code:
int icon = R.drawable.ic_launcher;
        long when = System.currentTimeMillis();
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notification = new Notification(icon, message, when);

        String title = "TITLE";

        Intent notificationIntent = new Intent(context, Activity.class);
        notificationIntent.putExtra("extras", extra); //Optional
        // set intent so it does not start a new activity
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
        notification.setLatestEventInfo(context, title, message, intent);
        notification.flags |= Notification.FLAG_AUTO_CANCEL;

        // Play default notification sound
        notification.defaults |= Notification.DEFAULT_SOUND;

        // Vibrate if vibrate is enabled
        notification.defaults |= Notification.DEFAULT_VIBRATE;
        notificationManager.notify(0, notification);
 
Danke für die Antworten.
Ich habe mich bis jetzt kaum mit Intents beschäftigt.
Wie kann ich jetzt zum Beispiel 'finish()' beim klick auf die Benachrichtigung öffnen?
 
Häh? Warum und wo willst Du finish() mit der Notification aufrufen? finish() beendet die Activity gleich wieder und wenn Du eine Methode finish() gennant hast, würde ich sie umbenennen...

The final call you receive before your activity is destroyed. This can happen either because the activity is finishing (someone called finish() on it, or because the system is temporarily destroying this instance of the activity to save space. You can distinguish between these two scenarios with the isFinishing() method.

Quelle: http://developer.android.com/reference/android/app/Activity.html
 
Von mir aus kann die Methode auch einen anderen Namen haben^^.
Ich möchte einfach nur wissen wie ich durch den Klick auf die Benachrichtigung eine Methode "aktiviere" zb. 'public void stopMusic(){...}
Hab auch schon gegoogelt aber mir wird es einfach nicht klar wie es funktioniert:unsure:
 
Du gibst der Notification einen von dir gewählten Intent mit der beim Klick auf die Notification mitgesendet werden soll. In der Activity überprüfst du dann ob im bundle etwas ist, wenn ja ob da dein Intent dabei liegt und wenn auch das mit ja beantwortet wird, dann rufst du dort deine Methode auf.


Bei sowas wie "mir wird es einfach nicht klar wie es funktioniert" kann man nicht helfen. Wenn du genau sagst woran es hapert, dann(!) kann man dir genau weiterhelfen, aber wenn es an allem hapert, wo soll man dann mit dem Erklären anfangen? :(
 
Ich hab es jetzt mal so gemacht :

Meine Notification:
Code:
Intent intent = new Intent(this, MainActivity.class);
        intent.putExtra("abc", "abc");
        PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
        @SuppressWarnings("deprecation")
        Notification noti = new Notification.Builder(this)
        .setTicker("Ticker Title")
        .setContentTitle("Content Title")
        .setContentText("Notification content.")
        .setSmallIcon(R.drawable.ic_launcher)
        .setContentIntent(pIntent).getNotification();
        noti.flags=Notification.FLAG_AUTO_CANCEL;

        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        notificationManager.notify(0, noti);
Code:
@Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        
        Bundle extras = getIntent().getExtras(); 
        String userName;

        if (extras != null) {
            userName = extras.getString("abc");
            // and get whatever type user account id is
            Toast.makeText(getApplicationContext(), userName, Toast.LENGTH_LONG).show();
        }
Theoretisch müsste es eigentlich funktionieren oder^^?
Aber ich bekomme hier keine Meldung --> ' Toast.makeText(getApplicationContext(), userName, Toast.LENGTH_LONG).show();'
 
Gib dem Intent auch noch eine Boolean-Wert mit. Wenn true --> Rufe Funktion stopMusic() auf. Ich packe die getIntent() Funktion immer in die onCreate() rein - funktioniert wunderbar!

Code:
intent.putExtra("stopMusic", true);
Code:
Intent i = getIntent();

String username = i.getStringExtra("username");
boolean stopMusic = i.getBooleanExtra("stopMusic", false); //false ist der Standard-Wert, wenn stopMusic im Bundle nicht vorhanden ist.

if (stopMusic)
     stopMusic();
 
Perfekt hat endlich geklappt danke:smile:
 

Ähnliche Themen

AnnaBauer21
Antworten
0
Aufrufe
596
AnnaBauer21
AnnaBauer21
Zurück
Oben Unten