Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alive Getx Controller in killed state #3091

Open
danishrafiqe opened this issue May 7, 2024 · 0 comments
Open

Alive Getx Controller in killed state #3091

danishrafiqe opened this issue May 7, 2024 · 0 comments

Comments

@danishrafiqe
Copy link

class SimpleChatController extends GetxController
with WidgetsBindingObserver
implements DataChangeEvents {
/// xmpp Connection
XmppConnection? xmppConnection;
}
this is my controller class
and this is my background service class
i want to make alive this controller alive in killed not disposing him self when app is killed
final flutterBackgroundService = FlutterBackgroundService();

Future initializeService() async {
/// OPTIONAL, using custom notification channel id
const AndroidNotificationChannel channel = AndroidNotificationChannel(
'my_foreground', // id
'MY FOREGROUND SERVICE', // title
description:
'This channel is used for important notifications.', // description
importance: Importance.low, // importance must be at low or higher level
);

final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();

if (Platform.isIOS || Platform.isAndroid) {
await flutterLocalNotificationsPlugin.initialize(
const InitializationSettings(
iOS: DarwinInitializationSettings(),
android: AndroidInitializationSettings('ic_bg_service_small'),
),
);
}

await flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin>()
?.createNotificationChannel(channel);

await flutterBackgroundService.configure(
androidConfiguration: AndroidConfiguration(
// this will be executed when app is in foreground or background in separated isolate
onStart: onStart,

  // auto start service
  autoStart: true,
  isForegroundMode: true,

  autoStartOnBoot: true,

  notificationChannelId: 'my_foreground',
  initialNotificationTitle: 'D-iD Connect',
  initialNotificationContent: 'Syncing...',
  foregroundServiceNotificationId: 888,
),
iosConfiguration: IosConfiguration(
  // auto start service
  autoStart: true,

  // this will be executed when app is in foreground in separated isolate
  onForeground: onStart,

  // you have to enable background fetch capability on xcode project
  onBackground: onIosBackground,
),

);
}

@pragma('vm:entry-point')
Future onIosBackground(ServiceInstance service) async {
WidgetsFlutterBinding.ensureInitialized();
DartPluginRegistrant.ensureInitialized();

SharedPreferences preferences = await SharedPreferences.getInstance();
await preferences.reload();
final log = preferences.getStringList('log') ?? [];
log.add(DateTime.now().toIso8601String());
await preferences.setStringList('log', log);

return true;
}

@pragma('vm:entry-point')
void onStart(ServiceInstance service) async {
// Only available for flutter 3.0.0 and later
DartPluginRegistrant.ensureInitialized();

/// OPTIONAL when use custom notification
final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();

if (service is AndroidServiceInstance) {
service.on('setAsForeground').listen((event) {
service.setAsForegroundService();
});

service.on('setAsBackground').listen((event) {
  service.setAsBackgroundService();
});

}

service.on('stopService').listen((event) {
print('Stoppingggggggggggggasasasasasas');
service.stopSelf();
});

// bring to foreground
Timer.periodic(const Duration(seconds: 3), (timer) async {
if (service is AndroidServiceInstance) {
if (await service.isForegroundService()) {
/// OPTIONAL for use custom notification
/// the notification id must be equals with AndroidConfiguration when you call configure() method.

    // if you don't using custom notification, uncomment this
    // service.setForegroundNotificationInfo(
    //   title: "D-iD Connect",
    //   content: "Sync....",
    // );
  }
}

/// you can see this log in logcat
print('FLUTTER BACKGROUND SERVICE: ${DateTime.now()}');

// test using external plugin
final deviceInfo = DeviceInfoPlugin();
String? device;
if (Platform.isAndroid) {
  final androidInfo = await deviceInfo.androidInfo;
  device = androidInfo.model;
}

if (Platform.isIOS) {
  final iosInfo = await deviceInfo.iosInfo;
  device = iosInfo.model;
}

service.invoke(
  'update',
  {
    "current_date": DateTime.now().toIso8601String(),
    "device": device,
  },
);

});
}
@jasonlaw @lsm @lemps @alindeman

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant