background service

This commit is contained in:
eskimo 2025-04-04 18:27:57 -04:00
parent eb3f3afa60
commit 16388ea9b9
4 changed files with 95 additions and 5 deletions

View File

@ -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"

View File

@ -1,2 +1,11 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC"/>
<application>
<service
android:name=".SocketForegroundService"
android:enabled="true"
android:exported="false"
android:foregroundServiceType="dataSync" />
</application>
</manifest>

View File

@ -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;
}
}

View File

@ -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);
}
}