问题描述
在我的 BaseApplication 类中,我尝试了此代码,并且能够根据需要设置标志。
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。