JDK-8264786 : [macOS] All Swing/AWT apps cause Allow Notifications prompt to appear when app is launched
  • Type: Bug
  • Component: client-libs
  • Sub-Component: java.awt
  • Affected Version: 10,11,13,15,16,17
  • Priority: P3
  • Status: Closed
  • Resolution: Fixed
  • OS: os_x
  • CPU: x86_64
  • Submitted: 2021-03-31
  • Updated: 2021-06-28
  • Resolved: 2021-05-03
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 11 JDK 13 JDK 15 JDK 16 JDK 17
11.0.12-oracleFixed 13.0.8Fixed 15.0.4Fixed 16.0.2Fixed 17 b21Fixed
Related Reports
Relates :  
Description
ADDITIONAL SYSTEM INFORMATION :
MacOS 10.15.7
OpenJDK 11.0.9 and 16.0.0 (but not on 9.0.7.1).

A DESCRIPTION OF THE PROBLEM :
After the implementation of JDK-8187639, all Swing/AWT apps now cause the following prompt to appear on MacOS when the app is first launched:

"java" Notifications
Notifications may include alerts, sounds, and icon badges.
Don't Allow/Allow?

The prompt confuses/annoys users, since our app should not be causing any notifications to happen.

The bug appears to have been introduced with JDK-8187639. I believe the permission popup appears in response to the following call in src/java.desktop/macosx/native/libosxapp/NSApplicationAWT.m:
[[NSUserNotificationCenter defaultUserNotificationCenter] setDelegate:self];

I managed to cause a similar permission popup to appear from a standalone Objective-C app that just performs the call above. So this seems likely to be the cause.

The suggested fix is to only call NSUserNotificationCenter.setDelegate if the TrayIcon facility is actually used, i.e. in src/java.desktop/macosx/native/libawt_lwawt/awt/CTrayIcon.m instead of always when the native Toolkit is being initialized.


REGRESSION : Last worked in version 8

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
1) Open "System Preferences" and then the "Notifications" pane. If there is any entry or entries for "java" in the panel to the left, select each entry and press the Delete key (Fn+Backspace) to reset the notification prompt, to ensure that it pops up in the next step.
2) Run the attached trivial AWT app, which just calls Toolkit.getDefaultToolkit(). Any Swing or AWT action would work.


EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
The app should exit uneventfully, with no user-visible permission popup.
ACTUAL -
An OS-level prompt pops up, asking the user to permit notifications from "java".

---------- BEGIN SOURCE ----------
import java.awt.Toolkit;
 
public class SpuriousNotificationExhibit {
  public static final void main(String args[]) throws Exception {
    // Any Swing or AWT action which would cause the toolkit to be initialized would work here.
    Toolkit.getDefaultToolkit();
  }
}
---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
No workaround exists. Though obviously the user can click away the popup.

FREQUENCY : always



Comments
Verified from submitter: ================= I just tested the latest JDK 17 build (openjdk-17-ea+24_macos-x64_bin.tar.gz) on MacOS 10.15.7 and can confirm the bug is resolved. I tested running our Java application both outside and inside an application bundle (outside the bundle, the MacOS would previously ask for permissions for "java", while inside a bundle, MacOS would previously ask for permissions for name of our application. I switched between the 17-ea+24 version and an earlier OpenJDK build I had lying around (17-ea+20), and can confirm that the problem appeared on the older build and disappeared on the newer build. MacOS no longer asks permission to enable notifications. Very good! Thank you for the very quick resolution to this issue!
03-06-2021

looks like Sergey and Christoph just had race condition on updating labels, returning jdk16u-fix-request back to labels list
05-05-2021

Fix Request (11u): The issue affects jdk11u as well, didn't apply cleanly mostly due to context code difference. After fix applied, request for notification permission doesn't appear anymore on simple awt app. Testing: tier1/tier2 Review thread - https://mail.openjdk.java.net/pipermail/jdk-updates-dev/2021-May/006049.html
04-05-2021

Fix Request (13u): The issue affects jdk13u as well, applies almost cleanly, only copyright year issue once. After fix applied, request for notification permission doesn't appear anymore on simple awt app.
04-05-2021

Fix Request (15u): The issue affects jdk15u as well, applies almost cleanly, only copyright year issue once. After fix applied, request for notification permission doesn't appear anymore on simple awt app.
04-05-2021

Changeset: 020236cb Author: Sergey Bylokhov <serb@openjdk.org> Date: 2021-05-03 20:53:35 +0000 URL: https://git.openjdk.java.net/jdk/commit/020236cb9825bf4fa91a495a179623e3fcdc0149
03-05-2021

Testing is in progress
27-04-2021

Note that Apple recommends using UNUserNotificationCenter instead of NSUserNotificationCenter, 10.14+
27-04-2021

We should move the code for notifications to the "CTrayIcon.m", but some additional checks will be needed since it could be possible that CTrayIcon will be used in an embedded mode where the main application will be different from NSApplicationAWT.
26-04-2021

Additional Information from submitter: =========================== Yes, the warning occurs once for every JDK instance (but for any kind of Swing or AWT app, as long as the toolkit gets initialized). In our case we bundle OpenJDK inside an ".app" bundle, so we get a separate prompt from other Java apps. I have now confirmed that the prompt appears even when the app bundle containing the bundled JDK is signed and "notarized" (per https://developer.apple.com/documentation/xcode/notarizing_macos_software_before_distribution ). > Seems to me every new app I run on macOS asks for this permission. Case-in-point, here's a Tweet from yesterday with someone making fun of an app for doing so: https://twitter.com/rsnous/status/1380001863345430531?s=20 (Not sure if that one was using Java in any way or not, though.)
09-04-2021

Caused by the security enhancements in the new macOS.
08-04-2021

Seems to me every new app I run on macOS asks for this permission. And I expect the submitter really means once for every JDK instance, not once for every Java app.
06-04-2021