How to prevent alert with incoming OneSignal notification?

Using this code line, I resolved my issue.

OneSignal.inFocusDisplaying(2);

It's changed in OneSignal 4.0.

For Kotlin:

OneSignal.setNotificationWillShowInForegroundHandler { notificationReceivedEvent ->
    notificationReceivedEvent.complete(null)
}

For Java:

OneSignal.setNotificationWillShowInForegroundHandler(new NotificationWillShowInForegroundHandler() {
  @Override
  void notificationWillShowInForeground(OSNotificationReceivedEvent notificationReceivedEvent) {    
     notificationReceivedEvent.complete(null);
  }
});

From The SDK documentation - When you startInit OneSignal, make sure to call inFocusDisplaying with "None" to disable OneSignal's in app AlertBox.

also on NotificationReceivedHandler section -

Important behavior notes - If you will be displaying your own in app message when a notification is received make sure to call inFocusDisplaying with None to disable OneSignal's in app AlertBox.