2

Создаётся и выводится на экран уведомление, при нажатии на которое запускается activity NotificationActivity (код немного упростил).

    Intent notificationIntent = new Intent(context, NotificationActivity.class);
    notificationIntent.putExtra("key_text", "notification text");
    PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);

    Notification.Builder builder = new Notification.Builder(context);

    builder.setContentIntent(contentIntent)
            .setSmallIcon(smallIconId)
            .setLargeIcon(BitmapFactory.decodeResource(res, bigIconId))
            .setTicker(res.getString(R.string.warning))
            .setWhen(System.currentTimeMillis())
            .setAutoCancel(true)
            .setContentText(message);

    Notification notification = builder.build();

    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

    notificationManager.notify(NOTIFY_ID, notification);

Проблема в том, что при этом запускается и основная activity приложения MainActivity.

Как сделать так, чтобы запускалась только NotificationActivity при нажатии на уведомление?

1
  • Создайте свой ресивер (broadcast receiver) и в нем обрабатывайте, что вам нужно открыть при получении определенного нотификейшена
    – Nuclominus
    28 июл 2015 в 13:00

1 ответ 1

2

Можно вот эти флаги добавить к интенту. Тогда в момент нажатия будет запускаться только NotificationActivity. Но ничего не будет мешать запустить MainActivity из NotificationActivity.

    notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
    notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.