Skip to content

Commit

Permalink
Merge pull request ddnet#9450 from Robyt3/Android-Server-Notification…
Browse files Browse the repository at this point in the history
…-Permission-Fix

Fix server not starting on Android 12 and older versions, refactoring
  • Loading branch information
def- authored Dec 30, 2024
2 parents 1b5dbe0 + aa73de2 commit 9ece28e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 16 deletions.
22 changes: 8 additions & 14 deletions scripts/android/files/java/org/ddnet/client/ServerService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

import java.io.File;

import androidx.core.app.RemoteInput;
import androidx.core.app.NotificationCompat;
import androidx.core.app.RemoteInput;
import androidx.core.app.ServiceCompat;

import android.app.*;
import android.content.*;
Expand Down Expand Up @@ -63,19 +64,12 @@ public void onCreate() {

createNotificationChannel();

Notification notification = createRunningNotification();
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
startForeground(
NOTIFICATION_ID,
notification,
ServiceInfo.FOREGROUND_SERVICE_TYPE_MANIFEST
);
} else {
startForeground(
NOTIFICATION_ID,
notification
);
}
ServiceCompat.startForeground(
this,
NOTIFICATION_ID,
createRunningNotification(),
ServiceInfo.FOREGROUND_SERVICE_TYPE_MANIFEST
);

thread = new NativeServerThread(this);
thread.start();
Expand Down
5 changes: 3 additions & 2 deletions src/android/android_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,9 @@ bool StartAndroidServer()
{
// We need the notification-permission to show a notification for the foreground service.
// We use SDL for this instead of doing it on the Java side because this function blocks
// until the user made a choice, which is easier to handle.
if(!SDL_AndroidRequestPermission("android.permission.POST_NOTIFICATIONS"))
// until the user made a choice, which is easier to handle. Only Android 13 (API 33) and
// newer support requesting this permission at runtime.
if(SDL_GetAndroidSDKVersion() >= 33 && !SDL_AndroidRequestPermission("android.permission.POST_NOTIFICATIONS"))
{
return false;
}
Expand Down

0 comments on commit 9ece28e

Please sign in to comment.