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



我已经设置了一个加载片段的导航抽屉应用程序,我还实现了Parse.com推送通知。我通过自定义ParsePushBroadcastReceiver接收通知。我现在想要的是,当我点击推送通知时,转到导航抽屉中的特定片段。我已经尝试了无数种方法,但除了第一个片段之外,似乎没有加载。我知道我必须首先启动我的MainActivity,在推送点击时传递一个意向,然后在我的活动类中检查该意向(尽管我不确定我应该在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();
        }
    }

除了确保获得正确的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
}

最新更新