Мне нужна простая функция: когда приложение находится в выключенном состоянии, я хочу, чтобы оно открывало браузер вместо приложения, когда пользователь нажимает на уведомление.
public void showNotification(Context context) {
// Define the URL to open
Uri webpage = Uri.parse("https://www.example.com");
// Create an intent to open the browser
Intent intent = new Intent(Intent.ACTION_VIEW, webpage);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // Ensure the browser is opened, not your app
intent.setPackage("com.android.chrome"); // Optional: Specify Chrome (or remove for default browser)
// Create the PendingIntent
PendingIntent pendingIntent = PendingIntent.getActivity(
context,
0,
intent,
PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE
);
// Build the notification
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, "channel_id")
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("Open Browser")
.setContentText("Click to view the website.")
.setContentIntent(pendingIntent)
.setAutoCancel(true);
// Display the notification
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
notificationManager.notify(1, builder.build());
}
Мне нужна простая функция: когда приложение находится в выключенном состоянии, я хочу, чтобы оно открывало браузер вместо приложения, когда пользователь нажимает на уведомление. [code]public void showNotification(Context context) { // Define the URL to open Uri webpage = Uri.parse("https://www.example.com");
// Create an intent to open the browser Intent intent = new Intent(Intent.ACTION_VIEW, webpage); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // Ensure the browser is opened, not your app intent.setPackage("com.android.chrome"); // Optional: Specify Chrome (or remove for default browser)
// Build the notification NotificationCompat.Builder builder = new NotificationCompat.Builder(context, "channel_id") .setSmallIcon(R.drawable.notification_icon) .setContentTitle("Open Browser") .setContentText("Click to view the website.") .setContentIntent(pendingIntent) .setAutoCancel(true);