Android - 点击推送通知时切换标签

问题描述

我在我的应用程序类中设置了以下内容

private class ExampleNotificationopenedHandler implements Onesignal.NotificationopenedHandler {
    
    @Override
    public void notificationopened(OSNotificationopenResult result) {
        
        JSONObject data = result.notification.payload.additionalData;
        String customKey;

        if (data != null) {
            customKey = data.optString("type",null);
            if (customKey != null) {
                Intent intent = new Intent(getApplicationContext(),MainActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.putExtra("type",customKey);
                startActivity(intent);
            }
        }
    }
}

这成功加载了 MainActivity。但是,我的应用程序是带有片段的选项卡式应用程序。我需要应用根据打开的通知类型切换到特定选项卡。

在 MainActivity 我做了以下事情:

if (getIntent().getExtras() != null) {
        String deepLink = getIntent().getExtras().getString("type");
        if (deepLink.equals("Type 2")) {
            setTitle("Type 2");
            getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,new TypeTwoFragment(),"Type 2").commit();
        }
    } else {
        setTitle("Type 1");
        getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,new TypeOneFragment(),"Type 1").commit();
    }

但这不起作用。

如何切换标签页并加载适当的片段?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)