diff --git a/android/build.gradle b/android/build.gradle index eb4ac06..ccdb40b 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -19,10 +19,10 @@ apply plugin: 'com.android.library' android { namespace "software.eskimo.capacitor.sockets" - compileSdk project.hasProperty('compileSdkVersion') ? rootProject.ext.compileSdkVersion : 34 + compileSdk project.hasProperty('compileSdkVersion') ? rootProject.ext.compileSdkVersion : 35 defaultConfig { minSdkVersion project.hasProperty('minSdkVersion') ? rootProject.ext.minSdkVersion : 22 - targetSdkVersion project.hasProperty('targetSdkVersion') ? rootProject.ext.targetSdkVersion : 34 + targetSdkVersion project.hasProperty('targetSdkVersion') ? rootProject.ext.targetSdkVersion : 35 versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" diff --git a/android/src/main/AndroidManifest.xml b/android/src/main/AndroidManifest.xml index a2f47b6..626f16e 100644 --- a/android/src/main/AndroidManifest.xml +++ b/android/src/main/AndroidManifest.xml @@ -1,2 +1,11 @@ + + + + + diff --git a/android/src/main/java/software/eskimo/capacitor/sockets/SocketForegroundService.java b/android/src/main/java/software/eskimo/capacitor/sockets/SocketForegroundService.java new file mode 100644 index 0000000..0767732 --- /dev/null +++ b/android/src/main/java/software/eskimo/capacitor/sockets/SocketForegroundService.java @@ -0,0 +1,71 @@ +package software.eskimo.capacitor.sockets; + +import android.app.Notification; +import android.app.NotificationChannel; +import android.app.NotificationManager; +import android.app.Service; +import android.content.Context; +import android.content.Intent; +import android.content.pm.ServiceInfo; +import android.os.Build; +import android.os.IBinder; +import androidx.core.app.NotificationCompat; +import android.util.Log; + +public class SocketForegroundService extends Service { + private static final String CHANNEL_ID = "socket_channel"; + private static final String CHANNEL_NAME = "Socket Service Channel"; + + @Override + public void onCreate() { + super.onCreate(); + createNotificationChannel(); + } + + @Override + public int onStartCommand(Intent intent, int flags, int startId) { + // Retrieve the main app's "app_name" string resource + int appNameResId = getResources().getIdentifier("app_name", "string", getApplicationContext().getPackageName()); + String appName = (appNameResId != 0) ? getString(appNameResId) : "Socket Service"; + + int iconResId = getResources().getIdentifier("ic_notification", "drawable", getApplicationContext().getPackageName()); + if (iconResId == 0) { + iconResId = android.R.drawable.ic_dialog_info; + } + + Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID) + .setContentTitle(appName) + .setContentText("Background service is running") + .setSmallIcon(iconResId) + .build(); + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { + startForeground(1, notification, ServiceInfo.FOREGROUND_SERVICE_TYPE_DATA_SYNC); + } else { + startForeground(1, notification); + } + + Log.d("SocketForegroundService", "Foreground service started with app name: " + appName); + return START_STICKY; + } + + private void createNotificationChannel() { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + NotificationChannel channel = new NotificationChannel( + CHANNEL_ID, + CHANNEL_NAME, + NotificationManager.IMPORTANCE_LOW + ); + channel.setDescription("Channel for socket foreground service"); + NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); + if (notificationManager != null) { + notificationManager.createNotificationChannel(channel); + } + } + } + + @Override + public IBinder onBind(Intent intent) { + return null; + } +} diff --git a/android/src/main/java/software/eskimo/capacitor/sockets/SocketsPlugin.java b/android/src/main/java/software/eskimo/capacitor/sockets/SocketsPlugin.java index ba1d3de..17573d2 100644 --- a/android/src/main/java/software/eskimo/capacitor/sockets/SocketsPlugin.java +++ b/android/src/main/java/software/eskimo/capacitor/sockets/SocketsPlugin.java @@ -1,5 +1,6 @@ package software.eskimo.capacitor.sockets; +import android.content.Intent; import android.util.Log; import com.getcapacitor.JSObject; import com.getcapacitor.Plugin; @@ -27,6 +28,9 @@ public class SocketsPlugin extends Plugin { boolean useTLS = call.getBoolean("useTLS", false); boolean acceptInvalidCertificates = call.getBoolean("acceptInvalidCertificates", false); + // Start the foreground service to keep the socket connection alive in the background + startSocketForegroundService(); + implementation.connect(id, host, port, useTLS, acceptInvalidCertificates); call.resolve(); } @@ -59,10 +63,16 @@ public class SocketsPlugin extends Plugin { public void notifyMessageListeners(String id, String message) { JSObject data = new JSObject(); data.put("id", id); - data.put("message", message); // Include the message content + data.put("message", message); - Log.d("SocketsPlugin", "Message to JS: " + data.toString()); // Log the data being sent to JS + Log.d("SocketsPlugin", "Message to JS: " + data.toString()); - notifyListeners("message", data); // Notify JS listeners of the 'message' event + notifyListeners("message", data); + } + + // Method to start the foreground service + private void startSocketForegroundService() { + Intent serviceIntent = new Intent(getContext(), SocketForegroundService.class); + getContext().startForegroundService(serviceIntent); } }