Skip to content

Commit

Permalink
Fix/android local notifications api 33 (#3735)
Browse files Browse the repository at this point in the history
* fix: added required permissions for local notifications on android 13

* Fix for android local notifications

#3729
  • Loading branch information
shannah authored Sep 17, 2023
1 parent e3e512d commit fd84cff
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -1951,6 +1958,11 @@ public void usesClassMethod(String cls, String method) {
" <uses-permission android:name=\"android.permission.FOREGROUND_SERVICE\" />\n");
}

if (postNotificationsPermission) {
permissions += permissionAdd(request, "\"android.permission.POST_NOTIFICATIONS\"",
" <uses-permission android:name=\"android.permission.POST_NOTIFICATIONS\" />\n");
}

if (capturePermission) {
String andc = request.getArg("android.captureRecord", "enabled");
if (request.getArg("and.captureRecord", andc).equals("enabled")) {
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit fd84cff

Please sign in to comment.