I have a website for my company and I needed to transform it into an app for cell phones. I made a webview because it meets my needs and makes my work easier. However, I am working on it so that the user can be asked if they want to receive notifications on their device. I managed to get Android to ask every first time the application was opened, however the Webview does not load regardless of the answer the user provides. If after this he closes and reopens the app everything works normally.
I followed a tutorial that guided me on how my code should look, I created two new classes for this, one that checked the Android version, and one that asked the user the question and returned to the main activity.
VerifySDK:
package com.zupermercado.zupermercado;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
public class VerifySDK extends AppCompatActivity {
int AndroidVersionSDK = Build.VERSION.SDK_INT;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_verify_sdk);
if(AndroidVersionSDK < 33){
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
}else{
Intent intent = new Intent(getApplicationContext(), NotificationVerify.class);
startActivity(intent);
}
}
}
and NotificationVerify
package com.zupermercado.zupermercado;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import static android.Manifest.permission.POST_NOTIFICATIONS;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
public class NotificationVerify extends AppCompatActivity {
private View view;
private static final int PERMISSION_REQUEST_CODE = 200;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notification_verify);
verifyNotifyPermission(view);
}
public void verifyNotifyPermission(View view){
if (!checkPermission()) {
requestPermission();
} else {
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}
}
private boolean checkPermission() {
int result = ContextCompat.checkSelfPermission(getApplicationContext(), POST_NOTIFICATIONS);
return result == PackageManager.PERMISSION_GRANTED;
}
private void requestPermission() {
ActivityCompat.requestPermissions(this, new String[]{POST_NOTIFICATIONS}, PERMISSION_REQUEST_CODE);
}
public void Intent(View view){
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}
}
Now, this is how my MainActivity looks like:
package com.zupermercado.zupermercado;
import static android.content.ContentValues.TAG;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.messaging.FirebaseMessaging;
public class MainActivity extends AppCompatActivity {
private WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CustomWebviewClient client = new CustomWebviewClient(this);
webView = findViewById(R.id.webview);
webView.loadUrl("https://zupermercado.com.br");
webView.setWebViewClient(client);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setDomStorageEnabled(true);
webView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
final String url = request.getUrl().toString();
if (url.contains("zupermercado.com.br"))
{
webView.loadUrl(url);
}
else {
Intent intent = new Intent(Intent.ACTION_VIEW, request.getUrl());
view.getContext().startActivity(intent);
return true;
}
return false;
}
}
);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event){
if(keyCode ==KeyEvent.KEYCODE_BACK && this.webView.canGoBack()){
this.webView.goBack();
return true;
/*Este código entre chaves serve para que o botão de voltar
* do Android sirva para voltar a página dentro do webview*/
}
return super.onKeyDown(keyCode, event);
}
class CustomWebviewClient extends WebViewClient {
private Activity activity;
public CustomWebviewClient(Activity activity){
this.activity = activity;
}
}
}
Sorry if i throw so much lines, i am no developer, just a curious guy who needs this app for his work and figure out how to do so.
My AndroidManifest too:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<application
android:allowBackup="true"
android:allowClearUserData="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcherzuper"
android:label="Zuper Mercado Online"
android:roundIcon="@mipmap/ic_launcherzuper_round"
android:supportsRtl="true"
android:theme="@style/Theme.Zupermercado"
android:usesCleartextTraffic="true"
tools:targetApi="31">
<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="@drawable/ic_zupernotification" />
<meta-data
android:name="com.google.firebase.messaging.default_notification_sound"
android:resource="@raw/zupersound" />
<meta-data
android:name="com.google.firebase.messaging.default_notification_color"
android:resource="@color/verdeagua" />
<service
android:name=".MyFirebaseMessagingService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<activity
android:name=".MainActivity"
android:exported="false">
</activity>
<activity
android:name=".NotificationVerify"
android:exported="false"/>
<activity
android:name=".VerifySDK"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
André Corsini D’Andréa is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.