【问题标题】:Navigate to specific Fragment on Push Notification click Android导航到推送通知上的特定片段单击 Android
【发布时间】:2016-04-07 13:59:30
【问题描述】:

我已经设置了一个用于加载片段的导航抽屉应用程序,我还实现了 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();
        }
    }

【问题讨论】:

  • 我忘了删除旧代码.. 现在怎么样
  • 这是我作为意图检查器所拥有的......基本上我想将我的 onPushOpen 方法链接到那个。
  • 您应该检查“store”而不是“menuFragment”

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


【解决方案1】:

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

【讨论】:

  • 它成功地检查了“store”字符串,但它从未找到它(总是转到“else”语句)
  • 在调用 putExtra() 之前记录 pushData.getString("data") 的值
  • @Rayyan 那仍然不正确。 “存储”是键,pushData.getString("data") 是数据。为什么只用硬编码的字符串“数据”替换它?此外,调用getStringExtra("data") 是错误的,因为您没有“数据”作为任何附加功能的键。
  • 好的,所以我尝试了一种检查推送意图的新方法,并且我已将我的问题更新为这种新方法。
猜你喜欢
  • 1970-01-01
  • 2019-04-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多