0

Ich habe kürzlich an Benachrichtigungen in My Android App gearbeitet.Öffnen eines Fragments onclick of Notification Button

Da meine Anwendung haben nur 1-Aktivität mit einigen Fragmenten ..

Also, meine eine der Schaltfläche auf der Benachrichtigung zu handhaben, so dass es ein Fragment öffnen kann und eine Methode aufrufen (oder es kann ein Anruf Methode, die mein Code enthält alle einschließlich das Fragment)

im Zuge dessen in den Aufbau einer Meldung und mit Arbeits Taste Aber mit einer Einschränkung sucessfull ich bin Eröffnung, dass die Schaltfläche nur

hier jede Aktivität zu öffnen gehandhabt werden kann ist Mein Code der Benachrichtigung, Bitte werfen Sie einen Blick darauf und Bitte legen nahe, wie soll ich es tun ..

public int getNotification(View view, String Data) { 

    // NotificationManager notificationManager = (NotificationManager) getActivity().getSystemService(Context.NOTIFICATION_SERVICE); 
    Context context = view.getContext(); 
    Intent intent = new Intent(context.getApplicationContext(), MapsActivity.class); 
    intent.putExtra("Notification","1"); 
    PendingIntent pendingIntent = PendingIntent.getActivity(context.getApplicationContext(), 0, intent, 0); 
    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 
    Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 

    //Intent for "Navigate" Button on Notification 
    String lat = String.valueOf(dest.latitude); 
    String lon = String.valueOf(dest.longitude); 
    String format = "geo:0,0?q=" + lat + "," + lon + "Destination Point"; 
    Uri uri = Uri.parse(format); 
    Intent intentNavigate = new Intent(Intent.ACTION_VIEW, uri); 
    PendingIntent pIntentNavigate = PendingIntent.getActivity(context.getApplicationContext(), 0, intentNavigate, 0); 

    if (Build.VERSION.SDK_INT < 16) { 
     getToast("API below 16 ...notification not supported"); 

    } else { 
     //Bitmap icon = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_car); 
     Notification.Builder builder = new Notification.Builder(context); 
     //builder.setLargeIcon(icon); 
     builder.setSmallIcon(R.drawable.icar); 
     builder.setContentTitle("Car Rental Application"); 
     builder.setContentText(Data); 
     builder.setOngoing(true); 
     builder.setSound(soundUri); 
     builder.setContentIntent(pendingIntent); 
     builder.addAction(R.drawable.ic_gps, "NAVIGATE ", pIntentNavigate); 
     builder.addAction(R.drawable.ic_stop, "STOP RIDE", pendingIntent); 
     Notification notify = builder.build(); 

     notificationManager.notify(notificationID, notify); 

    } 

    return notificationID; 
} 

public void removeNotification() { 
    NotificationManager manager = (NotificationManager) getActivity().getSystemService(Context.NOTIFICATION_SERVICE); 
    manager.cancel(
      notificationID); 
    getToast("Notification Removed"); 
} 

ich meine zweite Aktion in der Mitteilung (STOP RIDE) Griff, die eine Funktion stopride nennen sollte() (Bitte fragen, ob weitere ref. erforderlich) Bitte helfen Sie mir für mich möglich zu machen .. Vielen Dank im Voraus

Antwort

0

Sie benötigen Werte von getIntent() bekommen MapsActivity Jetzt versuchen Sie MapsActivity auf „STOP RIDE“ Aktion mit dieser Absicht zu berufen -

 Intent intent = new Intent(context.getApplicationContext(), MapsActivity.class); 
     intent.putExtra("Notification","1"); 
    PendingIntent pendingIntent = PendingIntent.getActivity(context.getApplicationContext(), 0, intent, 0); 

Sie können den gleichen Schlüssel "Benachrichtigung" verwenden oder fügen Sie eine weitere Flagge in diesem wie -

intent.putExtra("key_stop",true); 

in MapsActivity's onCreate() Sie können Ihr Fragment durch die Überprüfung Ihrer Flagge instanziiert -

boolean stopFlag = getIntent().getBooleanExtra("key_stop",false); 

    if(stopFlag){ 
     //Launch your fragment 
    } 

hoffe, das hilft !!