6

通知バーの通知から Android アプリケーションでフラグメントを開始するにはどうすればよいですか?

独自のアクションを作成し、アクションをインテントに設定するというこの回答を実装しようとしましたが、それを使用する方法と、マニフェストに何かを追加するなど、さらに必要なものがわかりません。

コンテキスト、メッセージ、そしてアクションを受け取る通知クラスがあります。次に、そのアクションをフィルタリングして起動するフラグメントを決定したいのですが、アクティビティを起動するのではなく、フラグメントを起動する方法がわかりません。

これが私の Notifications.java クラスです(不完全):

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);
    }

}
4

1 に答える 1

7

したがって、これは実際には非常に簡単でした。他の誰かがこれを見るのを助けることができれば幸いです。

notifyこの関数にアクションを送信します。そのアクションを my に追加してintent、アクティビティを開始します。私の場合、起動アクティビティを開きます。これは、すべてのフラグメントがユーザーの操作に基づいてそのアクティビティ内から読み込まれるためです。そのため、次のようにアクションを使用して設定setActionし、アクティビティでそのアクションを使用します。

Notifications.javaのクラスはこれに変わりました:

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

    action = action.toUpperCase();

    // Create a pending intent to open the the application when the notification is clicked.
    //Restart the app.
    Intent launchIntent = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());

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

    PendingIntent pendingIntent = PendingIntent.getActivity(context, -1, launchIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    notification.when = System.currentTimeMillis();  
    notification.flags |= Notification.FLAG_AUTO_CANCEL; 

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

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

そしてactivity、フラグメントをロードする from で、アクションを取得してフィルタリングします。

Intent intent = getIntent();

try{
    String action = intent.getAction().toUpperCase();

    if(action != null){
        if(action.equalsIgnoreCase(getResources().getString(R.string.notification_action_friend))){
                goFrag(getResources().getInteger(R.integer.FRAG_A_INT));
            }
        if(action.equalsIgnoreCase(getResources().getString(R.string.notification_action_article))){
                goFrag(getResources().getInteger(R.integer.FRAG_B_INT));
            }
        if(action.equalsIgnoreCase(getResources().getString(R.string.notification_action_points))){
                goFrag(getResources().getInteger(R.integer.FRAG_C_INT));
            }
        if(action.equalsIgnoreCase(getResources().getString(R.string.notification_action_redeemable))){
                goFrag(getResources().getInteger(R.integer.FRAG_D_INT));
            }
        if(action.equalsIgnoreCase(getResources().getString(R.string.notification_action_dance))){
                goFrag(getResources().getInteger(R.integer.FRAG_E_INT));
            }
        }else{
            Log.d(TAG, "Intent was null");
        }
    }catch(Exception e){
        Log.e(TAG, "Problem consuming action from intent", e);              
    }

私のgoFrag関数では、必要なフラグメントがまだメモリ内にある場合 (ユーザーが以前にそこにいて、まだ破棄されていないことを意味します)、フラグメントを置き換えるか、必要な新しいフラグメントを作成します。

于 2013-08-23T06:04:42.173 に答える