微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决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 举报,一经查实,本站将立刻删除。