问题
I created an intent that is used for a notification (i use data messages payload of firebase messaging system) in a class that extends FirebaseMessagingService. The user clicks on the notification and URL link loads. I use onNewIntent at mainActivity to get the URL and load it. The thing is that its working only when the app is in foreground and background. When the app is closed and then the notification arrive then when the user clicks the notification the app opens but the URL link does not load. The main webpage loads in the webview at mainactivity but not the specified URL link from the notification. Anyone knows how to solve this?
This is the notification code and the intent
Uri defaultSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("URL", link);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent,PendingIntent.FLAG_UPDATE_CURRENT);
Random rand = new Random();
NotificationManager notificationManager = (NotificationManager)getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
int int_random = rand.nextInt(100);
String auto=Integer.toString(int_random);
String NOTIFICATION_CHANNEL_ID = "101"+auto;
Log.i(TAG, "NOTIFICATION CHANNEL ID: " + NOTIFICATION_CHANNEL_ID);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
@SuppressLint("WrongConstant") NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "Notification", NotificationManager.IMPORTANCE_MAX);
//Configure Notification Channel
notificationChannel.setDescription("Notifications");
notificationChannel.enableLights(true);
notificationChannel.setVibrationPattern(new long[]{200});
notificationChannel.enableVibration(false);
notificationManager.createNotificationChannel(notificationChannel);
}
Drawable drawable= ContextCompat.getDrawable(getApplicationContext(),R.drawable.notificationstoreicon);
Bitmap bitmap = ((BitmapDrawable)drawable).getBitmap();
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(getApplicationContext(), NOTIFICATION_CHANNEL_ID)
.setContentTitle( messageTitle )
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSound)
.setShowWhen(true)
.setLargeIcon(bitmap)
.setSmallIcon(R.drawable.transparenticon)
.setChannelId( NOTIFICATION_CHANNEL_ID )
.setContentIntent(pendingIntent)
.setWhen(System.currentTimeMillis())
.setPriority(Notification.PRIORITY_MAX);
notificationManager.notify(int_random, notificationBuilder.build());
This is onNewIntent in Mainactivity
super.onNewIntent(intent);
setIntent(intent);
// getIntent() should always return the most recent
intent = getIntent();
Bundle extras = intent.getExtras();
Log.i(TAG, extras.getString("URL"));
webview1.load(extras.getString("URL"));
This is the webview code at Mainactivity onCreate() where everytime the app loads i load the main webpage but i want to load another URL link every time the user clicks on the notification and the app is closed.
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.context = this.getApplicationContext();
AlarmManager alarmManager = (AlarmManager)getApplicationContext().getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(getApplicationContext(),AlarmReceiver.class);
PendingIntent pendingIntent =
PendingIntent.getBroadcast(getApplicationContext(), 10, intent,
PendingIntent.FLAG_NO_CREATE);
if (pendingIntent != null && alarmManager != null) {
alarmManager.cancel(pendingIntent);
pendingIntent.cancel();
}
webview1 = (WebView) findViewById(R.id.webview1);
String newUA="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36\n";
webview1.getSettings().setUserAgentString(newUA);
webview1.getSettings().setJavaScriptEnabled(true);
webview1.getSettings().setLoadWithOverviewMode(true);
webview1.getSettings().setUseWideViewPort(true);
webview1.getSettings().setDomStorageEnabled(true);
webview1.getSettings().setSupportZoom(true);
webview1.getSettings().setBuiltInZoomControls(true);
webview1.getSettings().setDisplayZoomControls(false);
webview1.getSettings().setPluginState(WebSettings.PluginState.ON);
webview1.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
webview1.setScrollbarFadingEnabled(false);
webview1.setWebViewClient(new WebViewClient() {
ProgressDialog progressDialog;
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
isRedirected = false;
}
public void onLoadResource (WebView view, String url) {
if (!isRedirected) {
pgsBar.setVisibility(view.VISIBLE);
}
}
}
webview1.loadUrl("https://www.google.com");
}
来源:https://stackoverflow.com/questions/64492329/open-specific-custom-data-url-link-of-notification-received-from-firebase-messag