我有人为我创建了一个非常基本的WebView应用程序,它有OneSignal通知。该应用程序启动时会显示启动画面,然后主页面上的两个按钮会将用户发送到不同的网址 - 在我的示例中,Google和Stack Overflow。
它工作正常,但我在尝试进行更改以便在点击通知时打开特定的网址,并且无法进行锻炼,尽管这里有大量阅读和OneSignal文档。作为最后的手段,我想我会发布我的代码,看看是否有人可以帮助这个新的应用程序编码!
基本上,我需要在ExampleNotificationOpenedHandler类的最底部修复launchURL代码。我通过OneSignal将URL作为附加数据发送,应用程序在launchURL变量中正确读取它(当我查看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);
}
}
}
}
}
我没有编写此代码,但任何帮助都会受到赞赏。
答案 0 :(得分:1)
按钮
按照你这样做 if (launchURL != null) {
Log.i("OneSignalExample", "launchURL value: " + launchURL);
webView.clearHistory();
webView.loadUrl(launchURL);
home.setVisibility(View.GONE);
ll_Web.setVisibility(View.VISIBLE);
}
我认为在为它设置url时不会创建webview,如果可以,则在onStart中添加动作侦听器
@Override
protected void onStart()
{
super.onStart();
OneSignal.startInit(this)
.setNotificationOpenedHandler(new ExampleNotificationOpenedHandler())
.init();
}
}
或至少在setContentView(R.layout.main);
之后
喜欢这里
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
OneSignal.startInit(this)
.setNotificationOpenedHandler(new
ExampleNotificationOpenedHandler())
.init();
答案 1 :(得分:0)
为了其他人搜索类似问题的好处,我的工作代码如下。作为一个新手,我不确定它是不是最好的解决方案,但是我正在为我做的测试工作。
再次感谢@nimi_moradi提供帮助我解决问题的建议。
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();
}
}