Reputation: 2972
I have a main activity and onCreate
method I open a WebView webView
object with a url.
I have another class MyFirebaseMessagingService extends FirebaseMessagingService
to handle FCM notification.
I would like to get current webview url in MyFirebaseMessagingService
class to take action.
How can I access current webview url in MyFirebaseMessagingService
?
MainActivity
public class MainActivity extends AppCompatActivity {
public WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = findViewById(R.id.webview);
WebViewClientImpl webViewClient = new WebViewClientImpl(this, linearLayoutProgressBar);
webView.setWebViewClient(webViewClient);
webView.loadUrl(AppConstants.mobileUrl);
}
}
Another class is MyFirebaseMessagingService
public class MyFirebaseMessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d(TAG, "From: " + remoteMessage.getFrom());
// How can I access webview.getUrl() here to check current url?
}
}
public class WebViewClientImpl extends WebViewClient {
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
}
}
Thank you
Upvotes: 0
Views: 109
Reputation: 5241
Create class to store current Url
public class UrlManager {
public static String url;
}
Update WebViewClientImpl
class, you should override shouldOverrideUrlLoading
to get current Url. If you want to make sure Page
loaded, You can update Url
inside onPageFinished
public class WebViewClientImpl extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
UrlManager.url = request.getUrl().toString();
return super.shouldOverrideUrlLoading(view, request);
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
}
}
In MyFirebaseMessagingService
you can call UrlManager.url
to get current Url
Upvotes: 1
Reputation: 1134
in my case, I use intent.putExtra
.
for example.
in FirebaseMessagingService class,
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d("From: " , remoteMessage.getFrom());
sendNotification(remoteMessage.getData().get("body"));
}
private void sendNotification(String messageBody) {
Dlog.e("messageBody = " + messageBody);
try {
JSONObject jsonBody = new JSONObject(messageBody);
int pushType = jsonBody.getJSONObject("data").getInt("pushType");
String pushTitle = jsonBody.getString("title");
String textBody = "";
String contentText = "";
PendingIntent pendingIntent = null;
String push_title = jsonBody.getString("title");
String boardContent = jsonBody.getJSONObject("data").getString("boardContent");
String boardUrl = jsonBody.getJSONObject("data").getString("boardUrl");
textBody = boardContent;
contentText = boardContent;
if (boardUrl.equals("")){
Intent intent = new Intent(this, SplashActivity.class);
pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}else {
Intent intent = new Intent(this, PushWebViewActivity.class);
intent.putExtra("push_title", push_title);
intent.putExtra("boardUrl", boardUrl);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
Bitmap icon2 = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
NotificationCompat.BigTextStyle bigText = new NotificationCompat.BigTextStyle();
bigText.bigText(textBody);
NotificationCompat.Builder notificationBuilder;
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// make Channel for over oreo
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel mChannel = new NotificationChannel("channel", "channel_nm", NotificationManager.IMPORTANCE_HIGH);
mChannel.setVibrationPattern(new long[]{500, 500, 500, 500});
mChannel.setLightColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary));
AudioAttributes att = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.build();
mChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
mChannel.enableVibration(true);
mChannel.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION), att);
notificationManager.createNotificationChannel(mChannel);
notificationBuilder = new NotificationCompat.Builder(getApplicationContext(), mChannel.getId());
} else {
notificationBuilder = new NotificationCompat.Builder(getApplicationContext());
}
notificationBuilder
.setLargeIcon(icon2)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(pushTitle)
.setContentText(contentText)
.setStyle(bigText)
.setLights(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary), 3000, 3000)
.setVibrate(new long[]{500, 500, 500, 500})
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setAutoCancel(true)
.setPriority(NotificationCompat.PRIORITY_MAX)
.setContentIntent(pendingIntent);
int random_num = (int) ((new Date().getTime() / 1000L) % Integer.MAX_VALUE);
notificationManager.notify(random_num, notificationBuilder.build());
} catch (JSONException e) {
e.printStackTrace();
}
}
and in Activity (where you make webview)
boardUrl = getIntent().getStringExtra("boardUrl");
webView.loadUrl(boardUrl);
using from intent.
Upvotes: 0