Android - Открыть конкретный фрагмент после нажатия на уведомление не работает - PullRequest
0 голосов
/ 11 июля 2020

Я делаю приложение Android и у меня проблема с уведомлением. В частности, когда я открываю приложение, я хочу установить контроль над расположением устройства и в некоторых случаях отправить уведомление. Уведомление приходит правильно, но при нажатии на это намерение не работает. Точнее, я хочу изменить фрагмент, когда я нажимаю на уведомление.

Вот как я отправляю уведомление:

int requestID = (int) System.currentTimeMillis();

Log.d("VerifyPositionForAlert", "chiamo funzione");
// Create an explicit intent for an Activity in your app
Intent intent = new Intent(MainActivity.this, this.getClass());
intent.setAction("NearToSupermarket");
//intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.setFlags(0);
PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, requestID, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext(), CHANNEL_ID)
        .setSmallIcon(R.drawable.logo)
        .setContentTitle("Sei vicino ad un supermercato")
        .setContentText("Tanti prodotti ti aspettano. Corri a fare la spesa!")
        .setStyle(new NotificationCompat.BigTextStyle()
                .bigText("Tanti prodotti ti aspettano. Corri a fare la spesa!"))
        .setPriority(NotificationCompat.PRIORITY_HIGH)
        .setContentIntent(pendingIntent)
        .setAutoCancel(true);
// notificationId is a unique int for each notification that you must define
notificationManager.notify(101, builder.build());

Так я проверяю действие Intent, но onResume - это не вызывается, когда я нажимаю на уведомление:

@Override
public void onResume() {
    Log.d("MainActivity", "onResume");
    super.onResume();
    String action = getIntent().getAction();
    if(action.equals("NearToSupermarket")){
      setCurrentFragment(Map.class);
    }
}

Как я могу решить эту проблему?

...