Open OneSignal URL to WebView of another activity - android

With OneSignal Notification, I'm sending a notification containing URL. In my ADDITIONAL DATA section, I'm sending KEY: openURL and VALUE: http://example.com.
I'm trying to open the link inside the WebView of my DailyDose activity.
Here, my ProfileActivity activity, I'm trying to recieves the notification and send it to DailyDose activity.
public class ProfileActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
OneSignal.startInit(this)
.inFocusDisplaying(OneSignal.OSInFocusDisplayOption.Notification)
.setNotificationOpenedHandler(new NotificationOpenHandler())
.unsubscribeWhenNotificationsAreDisabled(true)
.init();
}
class NotificationOpenHandler implements OneSignal.NotificationOpenedHandler {
#Override
public void notificationOpened(OSNotificationOpenResult result) {
OSNotificationAction.ActionType actionType = result.action.type;
JSONObject data = result.notification.payload.additionalData;
String openURL = getIntent().getStringExtra("openURL");
Intent intent = new Intent(getApplicationContext(), DailyDose.class);
intent.putExtra("openURL", openURL);
startActivity(intent);
}
}
}
And this is my DailyDose activity, where I'm trying to open the link inside the WebView:
public class DailyDose extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_daily_dose);
String openURL = getIntent().getStringExtra("openURL");
final TextView textView = findViewById(R.id.debug_view);
textView.setText("URL from additionalData: " + openURL);
WebView webView = findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient());
if (openURL == null)
webView.loadUrl("https://google.com");
else
webView.loadUrl(openURL);
But it's not working. Tapping on the notification, it just opens the ProfileActivity activity of the app. Doesn't do anything else. What am I doing wrong? I have been searching for a solution for a while but couldn't get anything that works. Will very much appreciate your suggestion.

When you click on the notification it opens the ProfileActivity because it is default behaviour of onesignal. It will open same activity that have initialization of onesignal. So first you need to disable this default behaviour of onesignal. Write below lines in your AndroidManifest file.
<application>
<meta-data android:name="com.onesignal.NotificationOpened.DEFAULT" android:value="DISABLE" />
</application>
Now run your project and check it will open DailyDose activity. Let me know if still you face any problems.
For more clarification on this please read onesignal's official document :
https://documentation.onesignal.com/docs/android-customizations#section-background-data-and-notification-overriding (Search : "Changing the open action of a notification" in the page).

Related

Open first Link searched against a keyword

I have 2 activities in my app, First Activity is simple editText and submit Button, When I press submit, it launches webView Activity and searches the keyword entered in the first activity. Now I want to open the first link Automatically in webview.
public class MainActivity extends AppCompatActivity {
WebView webView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = findViewById(R.id.webView);
webView.setWebViewClient(new WebViewClient());
String query = "firebase";// Get the text from EditText here
String url = "https://www.google.com/search?q="+query;
webView.loadUrl(url);
}
}

How to attach webView with another activity so that it opens the webPage related to different posts in Android studio

I am using webView in my application and have another activity with different posts, I want to attach my webView to those posts so that if I click on post 1 the webPage which is linked to it opens up and if I click on post 2 the webPage linked to that post opens up in same webView, is there any way to do so. Also I am using firebase as a database and fetching my web URL from the database. Please help.
I am using Android Studio
webView class file:
public class webViewNews extends AppCompatActivity {
private WebView webviewthis;
private DatabaseReference mdataRef;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview_page);
mdataRef = FirebaseDatabase.getInstance().getReference().child("webing");
webviewthis = (WebView) findViewById(R.id.webView_news);
webviewthis.setWebViewClient(new WebViewClient());
webviewthis.getSettings().setJavaScriptEnabled(true);
webviewthis.getSettings().setLoadsImagesAutomatically(true);
mdataRef.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
post2web webView = dataSnapshot.getValue(post2web.class);
webviewthis.loadUrl(webView.getWebViewPost());
}
#Override
public void onCancelled(DatabaseError databaseError) {
throw databaseError.toException();
}
});
}
}
Fetch the url of that post and pass it with intent to webview activity.
Instead of using activity for webview, it is better to open webview in fragment.
It would be faster and easier
try this create one method like this
public static void callWebview(Context context, String url) {
Intent i = new Intent(context, webViewNews.class);
i.putExtra("url", url);
context.startActivity(i);
}
when you need to start webview activity than just call this methos like this
callWebview(this,"https://stackoverflow.com");

How to use firefox focus to open a url when use intent?

I tried write a app for open a special url use "intent". But I want to use firefox focus to open it. How to use firefox focus to open a url when use intent? And I don't know the component name of firefox focus.
public class MainActivity_EmbedLink extends AppCompatActivity {
// Used to load the 'native-lib' library on application startup.
static {
System.loadLibrary("native-lib");
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main__embed_link);
String url = "http://123.com ";
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
startActivity(intent);
finish();
}
}
I managed to get it to work with Tasker following the screenshot.
I am sure you can convert the same to code.

How to handle webview click event in android studio?

I am using webview in my application to show canvas. On my webview there is click here button. I want to open new activity on click of this 'click here' button on webview. I dont know how to handle this click event.Need help as soon as possible.
Following is my webview class code:
public class TableWebView extends Activity {
private SharedPreferences sharedPreferences;
private String customer_id,hotel_id;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_table_web_view);
WebView wvtable= (WebView)findViewById(R.id.webViewTable);
wvtable.setWebViewClient(new MyViewClient());
WebSettings webSettings = wvtable.getSettings();
webSettings.setJavaScriptEnabled(true);
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
customer_id = sharedPreferences.getString("cust_id", "0");//if no customerid found automatically 0 will be used
hotel_id=sharedPreferences.getString("hotel_master_id","0");
wvtable.loadUrl("http://192.168.100.169/zoilo_admin/public/index.php/show-hotel-view?hotel_id="+hotel_id+"&customer_id="+customer_id+"&date_time="+ Util.selected_date_time);
}
And here is how it looks after opening that url in webview:
Binding JavaScript code to Android code
When developing a web application that's designed specifically for the WebView in your Android application, you can create interfaces between your JavaScript code and client-side Android code. For example, your JavaScript code can call a method in your Android code to start activity.
To bind a new interface between your JavaScript and Android code, call addJavascriptInterface(), passing it a class instance to bind to your JavaScript and an interface name that your JavaScript can call to access the class.
Caution: If you've set your targetSdkVersion to 17 or higher, you must add the #JavascriptInterface annotation to any method that you want available to your JavaScript (the method must also be public). If you do not provide the annotation, the method is not accessible by your web page when running on Android 4.2 or higher.
public class TableWebView extends Activity {
private SharedPreferences sharedPreferences;
private String customer_id,hotel_id;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_table_web_view);
WebView wvtable= (WebView)findViewById(R.id.webViewTable);
wvtable.setWebViewClient(new MyViewClient());
wvtable.addJavascriptInterface(new WebAppInterface(this), "Android");
WebSettings webSettings = wvtable.getSettings();
webSettings.setJavaScriptEnabled(true);
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
customer_id = sharedPreferences.getString("cust_id", "0");//if no customerid found automatically 0 will be used
hotel_id=sharedPreferences.getString("hotel_master_id","0");
wvtable.loadUrl("http://192.168.100.169/zoilo_admin/public/index.php/show-hotel-view?hotel_id="+hotel_id+"&customer_id="+customer_id+"&date_time="+ Util.selected_date_time);
}
public class WebAppInterface {
Context mContext;
/** Instantiate the interface and set the context */
WebAppInterface(Context c) {
mContext = c;
}
/** Show a toast from the web page */
#JavascriptInterface
public void startNewActivity() {
//startActivity(new Intent(this, youactivityname.class));
}
}
This creates an interface called Android for JavaScript running in the WebView. At this point, your web application has access to the WebAppInterface class. For example, here's some HTML and JavaScript that call your "startNewActivity" method of your class using the new interface when the user clicks a button:
<input type="button" value="Click Here" onClick="showActivity()" />
<script type="text/javascript">
function showActivity() {
Android.startNewActivity();
}
</script>

WebView - Want to open in app not in browser

I have a problem. I created some app, which should open my website, but when I run the app on my phone, its ask me in which browser I want to open it(Google Chrome...). But I want that website is open in app, not in browser... I'm working with Android Studio 1.1.0... Here is my code in MainActivity.java:
public class MainActivity extends ActionBarActivity {
private WebView MyWeb;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyWeb = (WebView) findViewById(R.id.WebView);
MyWeb.getSettings().setJavaScriptEnabled(true);
MyWeb.getSettings().setSaveFormData(true);
MyWeb.loadUrl("http://www.mywebsite.com");
} }
P.S. I changed the url to the fake one...
Add this line of code before your MyWeb.loadUrl
MyWeb.setWebViewClient(new WebViewClient());
This worked for me.!

Categories

Resources