簡體   English   中英

Android OneSignal,從通知中打開特定片段

[英]Android OneSignal , open specific fragment from notification

我想通過使用通知中的標題直接打開特定片段。

在我的 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";
            }
        });

我使用此標志在 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);
        }

由於某種原因它沒有工作。 如何收聽通知並打開特定片段?

我共享的代碼實際上是有效的,但我在 mainFragment 中使用了第二部分而不是 mainActivity。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM