Открыть браузер из push-уведомления Firebase без запуска приложения?Android

Форум для тех, кто программирует под Android
Ответить
Anonymous
 Открыть браузер из push-уведомления Firebase без запуска приложения?

Сообщение Anonymous »

Мне нужна простая функция: когда приложение находится в выключенном состоянии, я хочу, чтобы оно открывало браузер вместо приложения, когда пользователь нажимает на уведомление.

Код: Выделить всё

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());
}

Сейчас приложение открывается первым.

Подробнее здесь: https://stackoverflow.com/questions/792 ... pplication
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «Android»