导航到推送通知上的特定片段,单击Android

时间:2016-01-03 23:47:20

标签: android android-fragments android-intent android-studio push-notification

我已经设置了导航抽屉应用程序来加载片段,我还实施了Parse.com推送通知。我通过自定义 ParsePushBroadcastReceiver 接收通知。我现在想要的是当我点击推送通知时转到导航抽屉中的特定片段。我尝试了无数种方法,但似乎除了第一个片段之外没有加载。我知道我必须首先启动我的MainActivity,传递一个push-click意图,然后在我的活动类中检查该意图(虽然我不确定我是否应该在我的onCreate方法或一些newPushIntent方法下检查)。

以下是我的自定义 ParsePushBroadcastReceiver 类:

public class Receiver extends ParsePushBroadcastReceiver {

public static final String PARSE_DATA_KEY = "com.parse.Data";

@Override
protected void onPushReceive(Context context, Intent intent) {
    super.onPushReceive(context, intent);

    if (intent == null)
        return;

    try {
        JSONObject json = new JSONObject(intent.getExtras().getString("com.parse.Data"));

        try {
            JSONObject data = json.getJSONObject("data");
            String title = data.getString("title");
            String message = data.getString("message");
            showNotificationMessage(context, title, message);

        } catch (JSONException e) {
        }

    } catch (JSONException e) {
    }
}

@Override
protected void onPushDismiss(Context context, Intent intent) {
    super.onPushDismiss(context, intent);
}

@Override
protected void onPushOpen(Context context, Intent intent) {
    super.onPushOpen(context, intent);

    JSONObject pushData = null;

    try {
        pushData = new JSONObject(intent.getStringExtra(KEY_PUSH_DATA));
        Intent i = new Intent(context, MainActivity.class);
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        i.putExtra("store", pushData.getString("data"));
        context.startActivity(i);

    } catch (JSONException e) {
        e.printStackTrace();
    }

}


/**
 * Shows the notification message in the notification bar
 * If the app is in background, launches the app
 *
 * @param mContext
 * @param title
 * @param message
 */
private void showNotificationMessage(Context mContext, String title, String message) {
    // notification icon
    int icon = R.mipmap.ic_launcher; //This used to be MainActivity.class
    Intent intent = new Intent(mContext, MainActivity.class);

    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);

    PendingIntent resultPendingIntent =
            PendingIntent.getActivity(
                    mContext,
                    0,
                    intent,
                    PendingIntent.FLAG_CANCEL_CURRENT
            );

    NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
            mContext);
    Notification notification = mBuilder.setSmallIcon(icon).setTicker(title).setWhen(0)
            .setAutoCancel(true)
            .setContentTitle(title)
            .setContentText(message)
            .setStyle(inboxStyle)
            .setContentIntent(resultPendingIntent)
            .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
            .setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(), icon))
            .setContentText(message)
            .build();

    NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(101, notification);
    }
}

这是我检查意图的MainActivity.java onCreate方法:

FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

    Intent intent = this.getIntent();

    if (intent != null) {

        String menuFragment = getIntent().getStringExtra("store");

        if (menuFragment != null) {
            com.rayyan_nasr.apple.besgb.activity.Parse favoritesFragment = new com.rayyan_nasr.apple.besgb.activity.Parse();
            fragmentTransaction.replace(R.id.container_body, favoritesFragment);
            fragmentTransaction.commit();

        } else {
            MainFragment standardFragment = new MainFragment();
            fragmentTransaction.replace(R.id.container_body, standardFragment);
            fragmentTransaction.commit();
        }
    }

1 个答案:

答案 0 :(得分:0)

除了确保获得正确的Intent Extra之外,请确保在FragmentTransaction上调用commit()

String menuFragment = getIntent().getStringExtra("store"); //changed to "store"

FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
if (menuFragment != null)
{
    if (menuFragment.equals("favoritesMenuItem"))
    {
        com.rayyan_nasr.apple.besgb.activity.Parse favoritesFragment = new com.rayyan_nasr.apple.besgb.activity.Parse();
        fragmentTransaction.replace(R.id.container_body, favoritesFragment);
        fragmentTransaction.commit(); //added
    }
}
else
{
    HomeFragment standardFragment = new HomeFragment();
    fragmentTransaction.replace(R.id.container_body, standardFragment);
    fragmentTransaction.commit(); //added
}