Reputation: 37
I had someone create a very basic WebView app for me which has OneSignal notifications. The app launches with a splash screen and then two buttons on the main page that sends users to different URL's - in my example below, Google and Stack Overflow.
It's working fine but I'm trying to make changes to open a specific URL when a notification is tapped and can't workout how, despite lots of reading here and the OneSignal documentation. As a last resort, I thought I would post my code to see if anyone can help this newbiew to app coding !
Basically, I need to fix the launchURL code at the very bottom in the ExampleNotificationOpenedHandler class. I'm sending the URL as additional data via OneSignal and the app reads it correctly in the launchURL variable (when I view the logcat).
public class App extends Activity {
private static final String WEB_URL1 = "https://google.com";
private static final String WEB_URL2 = "https://stackoverflow.com";
View ll_Web;
WebView webView;
SwipeRefreshLayout mSwipeRefreshLayout;
RelativeLayout splash, home;
View ll_pView, pView;
/**
* ATTENTION: This was auto-generated to implement the App Indexing API.
* See https://g.co/AppIndexing/AndroidStudio for more information.
*/
private GoogleApiClient client;
/**
* ATTENTION: This was auto-generated to implement the App Indexing API.
* See https://g.co/AppIndexing/AndroidStudio for more information.
*/
private GoogleApiClient client2;
@SuppressLint("SetJavaScriptEnabled")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
OneSignal.startInit(this)
.setNotificationOpenedHandler(new ExampleNotificationOpenedHandler())
.init();
setContentView(R.layout.main);
ll_Web = findViewById(R.id.ll_Web);
webView = (WebView) findViewById(R.id.webView);
splash = (RelativeLayout) findViewById(R.id.splash);
home = (RelativeLayout) findViewById(R.id.home);
ll_pView = (View) findViewById(R.id.ll_pView);
pView = (View) findViewById(R.id.pView);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setSupportZoom(true);
webView.getSettings().setBuiltInZoomControls(false);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
webView.setScrollBarStyle(SCROLLBARS_OUTSIDE_OVERLAY);
webView.setScrollbarFadingEnabled(false);
webView.getSettings().setPluginState(PluginState.ON);
webView.getSettings().setAllowFileAccess(true);
webView.getSettings().setAppCacheEnabled(true);
webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
webView.setWebChromeClient(new WebChromeClient() {
@Override
public void onProgressChanged(WebView view, int progress) {
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(pView.getLayoutParams());
lp.weight = progress;
pView.setLayoutParams(lp);
ll_pView.setVisibility(progress == 100 ? View.GONE : View.VISIBLE);
}
});
webView.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
mSwipeRefreshLayout.setRefreshing(false);
ll_pView.setVisibility(View.GONE);
super.onPageFinished(view, url);
}
});
mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.activity_main_swipe_refresh_layout);
mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
webView.reload();
}
});
findViewById(R.id.btn1).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
webView.clearHistory();
webView.loadUrl(WEB_URL1);
home.setVisibility(View.GONE);
ll_Web.setVisibility(View.VISIBLE);
}
});
findViewById(R.id.btn2).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
webView.clearHistory();
webView.loadUrl(WEB_URL2);
home.setVisibility(View.GONE);
ll_Web.setVisibility(View.VISIBLE);
}
});
(new Handler()).postDelayed(new Runnable() {
@Override
public void run() {
home.setVisibility(View.VISIBLE);
splash.setVisibility(View.GONE);
}
}, 3 * 1000);
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
}
@Override
public void onBackPressed() {
if (ll_Web.getVisibility() == View.VISIBLE) {
if (webView.canGoBack() && !webView.getUrl().equals(WEB_URL1) && !webView.getUrl().equals(WEB_URL2)) {
webView.goBack();
} else {
home.setVisibility(View.VISIBLE);
if (Build.VERSION.SDK_INT < 18) {
webView.clearView();
} else {
webView.loadUrl("about:blank");
}
ll_Web.setVisibility(View.GONE);
}
} else
super.onBackPressed();
}
/**
* ATTENTION: This was auto-generated to implement the App Indexing API.
* See https://g.co/AppIndexing/AndroidStudio for more information.
*/
@Override
public void onStart() {
super.onStart();
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
//client2.connect();
}
@Override
public void onStop() {
super.onStop();
}
class ExampleNotificationOpenedHandler implements OneSignal.NotificationOpenedHandler {
// This fires when a notification is opened by tapping on it.
@Override
public void notificationOpened(OSNotificationOpenResult result) {
OSNotificationAction.ActionType actionType = result.action.type;
JSONObject data = result.notification.payload.additionalData;
String launchURL;
if (data != null) {
launchURL = data.optString("launchURL");
if (launchURL != null) {
Log.i("OneSignalExample", "launchURL value: " + launchURL);
// How do I send the LaunchURL to open webview???
// webView.loadUrl(launchURL);
}
}
}
}
}
I didn't write this code but any help would be appreciated.
Upvotes: 0
Views: 787
Reputation: 37
For the benefit of others searching for a similar issue, my working code is below. As a novice, I'm not sure if it's the best solution but it's working for me with the testing I've been doing.
Thanks again to @nimi_moradi for the advice which helped me solve my issue.
class ExampleNotificationOpenedHandler implements OneSignal.NotificationOpenedHandler {
// This fires when a notification is opened by tapping on it and
// puts the launchURL additional data into a string for use in the main activity
public static String launchURL;
@Override
public void notificationOpened(OSNotificationOpenResult result) {
OSNotificationAction.ActionType actionType = result.action.type;
JSONObject data = result.notification.payload.additionalData;
//public String launchURL;
if (data != null) {
launchURL = data.optString("launchURL");
if (launchURL != null) {
// Log.i("OneSignalExample", "launchURL value: " + launchURL);
}
}
}
}
public class App extends Activity {
private static final String WEB_URL1 = "https://google.com";
private static final String WEB_URL2 = "https://stackoverflow.com";
View ll_Web;
WebView webView;
SwipeRefreshLayout mSwipeRefreshLayout;
RelativeLayout splash, home;
View ll_pView, pView;
/**
* ATTENTION: This was auto-generated to implement the App Indexing API.
* See https://g.co/AppIndexing/AndroidStudio for more information.
*/
private GoogleApiClient client;
/**
* ATTENTION: This was auto-generated to implement the App Indexing API.
* See https://g.co/AppIndexing/AndroidStudio for more information.
*/
private GoogleApiClient client2;
@SuppressLint("SetJavaScriptEnabled")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
OneSignal.startInit(this)
.setNotificationOpenedHandler(new ExampleNotificationOpenedHandler())
.init();
setContentView(R.layout.main);
ll_Web = findViewById(R.id.ll_Web);
webView = (WebView) findViewById(R.id.webView);
splash = (RelativeLayout) findViewById(R.id.splash);
home = (RelativeLayout) findViewById(R.id.home);
ll_pView = findViewById(R.id.ll_pView);
pView = findViewById(R.id.pView);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setSupportZoom(true);
webView.getSettings().setBuiltInZoomControls(false);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
webView.setScrollBarStyle(SCROLLBARS_OUTSIDE_OVERLAY);
webView.setScrollbarFadingEnabled(false);
webView.getSettings().setPluginState(PluginState.ON);
webView.getSettings().setAllowFileAccess(true);
webView.getSettings().setAppCacheEnabled(true);
webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
webView.setWebChromeClient(new WebChromeClient() {
@Override
public void onProgressChanged(WebView view, int progress) {
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(pView.getLayoutParams());
lp.weight = progress;
pView.setLayoutParams(lp);
ll_pView.setVisibility(progress == 100 ? View.GONE : View.VISIBLE);
}
});
webView.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
mSwipeRefreshLayout.setRefreshing(false);
ll_pView.setVisibility(View.GONE);
super.onPageFinished(view, url);
}
});
mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.activity_main_swipe_refresh_layout);
mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
webView.reload();
}
});
//Splash screen and main menu (unless launchURL exists)
(new Handler()).postDelayed(new Runnable() {
@Override
public void run() {
if ("".equals(ExampleNotificationOpenedHandler.launchURL) || ExampleNotificationOpenedHandler.launchURL==null){
// Show home screen with menu buttons
home.setVisibility(View.VISIBLE);
}
// Show splash screen for three seconds
splash.setVisibility(View.GONE);
}
}, 3 * 1000);
// if launchURL contains data, open that
if (!"".equals(ExampleNotificationOpenedHandler.launchURL)) {
home.setVisibility(View.GONE);
ll_Web.setVisibility(View.VISIBLE);
webView.clearHistory();
webView.loadUrl(ExampleNotificationOpenedHandler.launchURL);
}
// Menu button one tapped - Google
findViewById(R.id.btn1).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
webView.clearHistory();
webView.loadUrl(WEB_URL1);
home.setVisibility(View.GONE);
ll_Web.setVisibility(View.VISIBLE);
}
});
// Menu button two tapped - Stack Overflow
findViewById(R.id.btn2).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
webView.clearHistory();
webView.loadUrl(WEB_URL2);
home.setVisibility(View.GONE);
ll_Web.setVisibility(View.VISIBLE);
}
});
}
@Override
public void onBackPressed() {
if (ll_Web.getVisibility() == View.VISIBLE) {
if (webView.canGoBack() && !webView.getUrl().equals(WEB_URL1) && !webView.getUrl().equals(WEB_URL2)) {
webView.goBack();
} else {
home.setVisibility(View.VISIBLE);
if (Build.VERSION.SDK_INT < 18) {
webView.clearView();
} else {
webView.loadUrl("about:blank");
}
ll_Web.setVisibility(View.GONE);
}
} else
super.onBackPressed();
}
@Override
public void onStart() {
super.onStart();
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
//client2.connect();
}
@Override
public void onStop() {
super.onStop();
}
}
Upvotes: 0
Reputation: 2618
do it like you with button
if (launchURL != null) {
Log.i("OneSignalExample", "launchURL value: " + launchURL);
webView.clearHistory();
webView.loadUrl(launchURL);
home.setVisibility(View.GONE);
ll_Web.setVisibility(View.VISIBLE);
}
i think webview is not created when set url for it , add action listener in onStart if you can
@Override
protected void onStart()
{
super.onStart();
OneSignal.startInit(this)
.setNotificationOpenedHandler(new ExampleNotificationOpenedHandler())
.init();
}
}
or at least after setContentView(R.layout.main);
like here
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
OneSignal.startInit(this)
.setNotificationOpenedHandler(new
ExampleNotificationOpenedHandler())
.init();
Upvotes: 1