简体   繁体   English

Android OneSignal,从通知中打开特定片段

[英]Android OneSignal , open specific fragment from notification

I want to directly open the specific fragment through using title from notification.我想通过使用通知中的标题直接打开特定片段。

In my BaseApplication class I tried this code and i was able to set the flag as i wanted.在我的 BaseApplication class 中,我尝试了此代码,并且能够根据需要设置标志。

   OneSignal.setLogLevel(OneSignal.LOG_LEVEL.VERBOSE, OneSignal.LOG_LEVEL.NONE);

        // OneSignal Initialization
        OneSignal.initWithContext(this);
        OneSignal.setAppId(ONESIGNAL_APP_ID);

        OneSignal.setNotificationOpenedHandler(result -> {
            Log.d("TAG", "notificationOpened: " + result.toString());

            if (result.getNotification().getTitle().toLowerCase().equals("first")) {
                fromNotifFlag = "first";
            } else if (result.getNotification().getTitle().toLowerCase().equals("second")) {
                fromNotifFlag = "second";
            }
        });

I used this flag to open the fragment in MainActivity(onCreate).我使用此标志在 MainActivity(onCreate) 中打开片段。

 if (BaseApplication.fromNotifFlag.equals("first")) {
            Navigation.findNavController(getCurrentFocus()).navigate(R.id.action_mainfragment_to_secondFragment);
        }

        if (BaseApplication.fromNotifFlag.equals("second")) {
            Navigation.findNavController(getCurrentFocus()).navigate(R.id.action_mainfragment_to_secondFragment);
        }

It didn't work for some reason.由于某种原因它没有工作。 How can i listen the notification and open a specific fragment?如何收听通知并打开特定片段?

The code i shared is actually working but i used the second part in my mainFragment instead of mainActivity.我共享的代码实际上是有效的,但我在 mainFragment 中使用了第二部分而不是 mainActivity。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM