From fd84cff2bd9ff950f06785d6a5054d39e95cc823 Mon Sep 17 00:00:00 2001 From: Steve Hannah Date: Sat, 16 Sep 2023 20:59:38 -0700 Subject: [PATCH] Fix/android local notifications api 33 (#3735) * fix: added required permissions for local notifications on android 13 * Fix for android local notifications https://github.com/codenameone/CodenameOne/issues/3729 --- .../impl/android/AndroidImplementation.java | 7 +++++- .../builders/AndroidGradleBuilder.java | 22 ++++++++++++------- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/Ports/Android/src/com/codename1/impl/android/AndroidImplementation.java b/Ports/Android/src/com/codename1/impl/android/AndroidImplementation.java index 6cc28d1870..bef52e0456 100644 --- a/Ports/Android/src/com/codename1/impl/android/AndroidImplementation.java +++ b/Ports/Android/src/com/codename1/impl/android/AndroidImplementation.java @@ -10472,7 +10472,12 @@ BackgroundFetch getBackgroundFetchListener() { } public void scheduleLocalNotification(LocalNotification notif, long firstTime, int repeat) { - + if (android.os.Build.VERSION.SDK_INT >= 33) { + if(!checkForPermission("android.permission.POST_NOTIFICATIONS", "This is required to receive notifications")){ + com.codename1.io.Log.e(new RuntimeException("Local notification was prevented the POST_NOTIFICATIONS permission was not granted by the user.")); + return; + } + } final Intent notificationIntent = new Intent(getContext(), LocalNotificationPublisher.class); notificationIntent.setAction(getContext().getApplicationInfo().packageName + "." + notif.getId()); notificationIntent.putExtra(LocalNotificationPublisher.NOTIFICATION, createBundleFromNotification(notif)); diff --git a/maven/codenameone-maven-plugin/src/main/java/com/codename1/builders/AndroidGradleBuilder.java b/maven/codenameone-maven-plugin/src/main/java/com/codename1/builders/AndroidGradleBuilder.java index 4d798a1d2f..ad99b0f1bf 100644 --- a/maven/codenameone-maven-plugin/src/main/java/com/codename1/builders/AndroidGradleBuilder.java +++ b/maven/codenameone-maven-plugin/src/main/java/com/codename1/builders/AndroidGradleBuilder.java @@ -86,7 +86,7 @@ public class AndroidGradleBuilder extends Executor { private String gradle8DistributionUrl = "https://services.gradle.org/distributions/gradle-8.1-bin.zip"; public boolean PREFER_MANAGED_GRADLE=true; - private boolean useGradle8 = true; + private boolean useGradle8 = false; private boolean useJava8SourceLevel = true; private File gradleProjectDirectory; @@ -276,6 +276,8 @@ public File getGradleProjectDirectory() { private boolean purchasePermissions; private boolean accessNetworkStatePermission; private boolean recieveBootCompletedPermission; + + private boolean postNotificationsPermission; private boolean getAccountsPermission; private boolean credentialsPermission; private boolean backgroundLocationPermission; @@ -735,7 +737,9 @@ public boolean build(File sourceZip, final BuildRequest request) throws BuildExc delTree(tmpFile); } tmpFile.mkdirs(); - File managedGradleHome = new File(path(System.getProperty("user.home"), ".codenameone", "gradle")); + String gradleHomeVersion = useGradle8 ? "8" : "6_5"; + File managedGradleHome = new File(path(System.getProperty("user.home"), ".codenameone", "gradle" + gradleHomeVersion)); + String gradleHome = System.getenv("GRADLE_HOME"); if (gradleHome == null && managedGradleHome.exists()) { gradleHome = managedGradleHome.getAbsolutePath(); @@ -1099,6 +1103,9 @@ public boolean build(File sourceZip, final BuildRequest request) throws BuildExc public void usesClass(String cls) { if (cls.indexOf("com/codename1/notifications") == 0) { recieveBootCompletedPermission = true; + if (targetSDKVersionInt >= 33) { + postNotificationsPermission = true; + } } if (cls.indexOf("com/codename1/capture") == 0) { capturePermission = true; @@ -1951,6 +1958,11 @@ public void usesClassMethod(String cls, String method) { " \n"); } + if (postNotificationsPermission) { + permissions += permissionAdd(request, "\"android.permission.POST_NOTIFICATIONS\"", + " \n"); + } + if (capturePermission) { String andc = request.getArg("android.captureRecord", "enabled"); if (request.getArg("and.captureRecord", andc).equals("enabled")) { @@ -3013,13 +3025,7 @@ public void usesClassMethod(String cls, String method) { " mNotifyBuilder.setCategory(\"Notification\");\n" + " }\n"; if (buildToolsVersionInt >= 26 && Integer.parseInt(targetNumber) >= 26) { - - - - - pushReceiverSourceCode += " com.codename1.impl.android.AndroidImplementation.setNotificationChannel(nm, mNotifyBuilder, context);\n"; - } pushReceiverSourceCode += " String[] messages = com.codename1.impl.android.AndroidImplementation.getPendingPush(messageType, context);\n"