单击基于 Fire base 的推送通知时在 WebView 中打开特定 URL 页面?
我使用Webview创建了一个Android应用程序并实现了Firebase推送通知,我还在发送推送通知时在firebase消息控制台中的自定义数据字段中传递了URL,但是当我单击通知时,它直接打开我的应用程序的主页。如何让它在点击通知时打开应用程序中的特定页面?
我使用了以下代码,但它仍然打开一个开放页面而不是特定页面。请推荐
这是我的代码
主要活动
`公共类 MainActivity 扩展 AppCompatActivity {
private WebView mywebView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mywebView=(WebView) findViewById(R.id.webview);
mywebView.setWebViewClient(new WebViewClient());
mywebView.loadUrl("https://exampe.blogspot.com/");
WebSettings webSettings=mywebView.getSettings();
webSettings.setJavaScriptEnabled(true);
}`
MyFirebaseMessagingService 类
公共类 MyFirebaseMessagingService 扩展 FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
// Extract the URL from the notification data
String url = remoteMessage.getData().get("url");
// Create a PendingIntent to open the URL in the webview
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("url", url);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
// Build and show the notification
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, "default_channel")
.setContentTitle(remoteMessage.getNotification().getTitle())
.setContentText(remoteMessage.getNotification().getBody())
.setSmallIcon(R.mipmap.ic_launcher)
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}
}
您需要从
Intent
获取您从PendingIntent
传递的URL,如下所示
public class MainActivity extends AppCompatActivity {
private WebView mywebView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mywebView=(WebView) findViewById(R.id.webview);
String url = getIntent.getStringExtra("url");
mywebView.setWebViewClient(new WebViewClient());
mywebView.loadUrl(url);
WebSettings webSettings=mywebView.getSettings();
webSettings.setJavaScriptEnabled(true);
}