Starten Sie ein Fragment in meiner Android-Anwendung über die Benachrichtigungsleiste

Wie starte ich ein Fragment in meiner Android-Anwendung aus einer Benachrichtigung in der Benachrichtigungsleiste?

Ich habe versucht, umzusetzendiese Antwort Wenn ich meine eigene Aktion erstelle und dann die Aktion auf die Absicht einstelle, bin ich mir nicht sicher, wie ich sie verwenden soll und was zusätzlich erforderlich ist - wie etwas zum Manifest hinzuzufügen.

Ich habe eine Benachrichtigungsklasse, die einen Kontext, eine Nachricht und dann eine Aktion empfängt. Ich möchte dann nach dieser Aktion filtern, um zu bestimmen, welches Fragment gestartet werden soll, aber ich weiß nicht, wie ein Fragment im Gegensatz zum Starten einer Aktivität gestartet werden soll.

Hier ist meine Notifications.java-Klasse (unvollständig):

public class Notifications {

    private Context mContext;

    public Notifications(Context context) {
        this.mContext = context;
    }

    public static void notify(Context context, String message, String  action) {

        //Action you invent should include the application package as a prefix — for example: "com.example.project.SHOW_COLOR".
        action = "my.package.name.here.frag."+action;

        //Construct a user message.
        String appName = context.getResources().getString(R.string.app_name);

        // Use the Notification manager to send notification
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        // Create a notification using android stat_notify_chat icon. 
        Notification notification = new Notification(R.drawable.ic_stat_notification, message, 0);

        //Sound, lights, vibration.
        //REMEMBER PERMISSIONS.
        notification.defaults |= Notification.DEFAULT_SOUND;
        notification.defaults |= Notification.DEFAULT_VIBRATE;
        notification.defaults |= Notification.DEFAULT_LIGHTS;

        // Create a pending intent to open the application when the notification is clicked.
        //Restart the app.
        Intent launchIntent = null;

        //Get the action and based on what the action is, launch the application displaying the appropriate fragment.
        if (action.equalsIgnoreCase("friend")){
            //New friend notification
            //Launch application displaying the list of friends


        }
        if (action.equalsIgnoreCase("article")){
            //New article has been posted
            //Launch application displaying the news feed fragment


        }
        if (action.equalsIgnoreCase("points")){
            //Points scored notification
            //Launch application displaying the user's profile


        }
        if (action.equalsIgnoreCase("redeemable")){
            //New redeemable is offered
            //Launch application displaying the list of redeemables


        }
        if (!action.equalsIgnoreCase("friend") 
                && !action.equalsIgnoreCase("article") 
                && !action.equalsIgnoreCase("points") 
                && !action.equalsIgnoreCase("redeemable")){
            //Not specific, so launch the application from scratch displaying the activity feed

            launchIntent = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());
        }



        if(action != null && launchIntent != null){         
            launchIntent.setAction(action);         
        }

        // Set the notification and register the pending intent to it
        notification.setLatestEventInfo(context, appName, message, pendingIntent);

        // Trigger the notification
        notificationManager.notify(0, notification);
    }

}