Skip to content

Initialize user defaults with a custom domain #20

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,8 @@ class ForegroundService: Service(), MethodChannel.MethodCallHandler {
override fun onDestroy() {
super.onDestroy()
destroyForegroundTask()
if (isSticky && serviceAction != ForegroundServiceAction.STOP) {

if (serviceAction != null && isSticky && && serviceAction != ForegroundServiceAction.STOP) {
Log.d(TAG, "The foreground service was terminated due to an unexpected problem. Set a restart alarm.")
setRestartAlarm()
}
Expand Down Expand Up @@ -202,17 +203,17 @@ class ForegroundService: Service(), MethodChannel.MethodCallHandler {
currFlutterEngine = FlutterEngine(this)

flutterLoader = FlutterInjector.instance().flutterLoader()
flutterLoader!!.startInitialization(this)
flutterLoader!!.ensureInitializationComplete(this, null)
flutterLoader?.startInitialization(this)
flutterLoader?.ensureInitializationComplete(this, null)

val messenger = currFlutterEngine!!.dartExecutor.binaryMessenger
val messenger = currFlutterEngine?.dartExecutor.binaryMessenger
backgroundChannel = MethodChannel(messenger, "flutter_foreground_task/background")
backgroundChannel!!.setMethodCallHandler(this)
backgroundChannel?.setMethodCallHandler(this)

val callbackInfo = FlutterCallbackInformation.lookupCallbackInformation(callbackHandle)
val appBundlePath = flutterLoader!!.findAppBundlePath()
val appBundlePath = flutterLoader?.findAppBundlePath()
val dartCallback = DartExecutor.DartCallback(assets, appBundlePath, callbackInfo)
currFlutterEngine!!.dartExecutor.executeDartCallback(dartCallback)
currFlutterEngine?.dartExecutor.executeDartCallback(dartCallback)
}

private fun startForegroundTask() {
Expand Down
42 changes: 20 additions & 22 deletions ios/Classes/service/BackgroundServiceManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,53 +66,51 @@ class BackgroundServiceManager: NSObject {

private func saveOptions(call: FlutterMethodCall) {
guard let argsDict = call.arguments as? Dictionary<String, Any> else { return }
let prefs = UserDefaults.standard
let prefs = UserDefaults(suiteName: USER_DEFAULTS_DOMAIN)

let notificationContentTitle = argsDict[NOTIFICATION_CONTENT_TITLE] as? String ?? ""
let notificationContentText = argsDict[NOTIFICATION_CONTENT_TEXT] as? String ?? ""
let showNotification = argsDict[SHOW_NOTIFICATION] as? Bool ?? true
let playSound = argsDict[PLAY_SOUND] as? Bool ?? false
let taskInterval = argsDict[TASK_INTERVAL] as? Int ?? 5000
let callbackHandle = argsDict[CALLBACK_HANDLE] as? Int64

prefs?.set(notificationContentTitle, forKey: NOTIFICATION_CONTENT_TITLE)
prefs?.set(notificationContentText, forKey: NOTIFICATION_CONTENT_TEXT)
prefs?.set(showNotification, forKey: SHOW_NOTIFICATION)
prefs?.set(playSound, forKey: PLAY_SOUND)
prefs?.set(taskInterval, forKey: TASK_INTERVAL)
prefs?.removeObject(forKey: CALLBACK_HANDLE)
prefs?.removeObject(forKey: CALLBACK_HANDLE_ON_RESTART)

prefs.set(notificationContentTitle, forKey: NOTIFICATION_CONTENT_TITLE)
prefs.set(notificationContentText, forKey: NOTIFICATION_CONTENT_TEXT)
prefs.set(showNotification, forKey: SHOW_NOTIFICATION)
prefs.set(playSound, forKey: PLAY_SOUND)
prefs.set(taskInterval, forKey: TASK_INTERVAL)
prefs.removeObject(forKey: CALLBACK_HANDLE)
prefs.removeObject(forKey: CALLBACK_HANDLE_ON_RESTART)
if callbackHandle != nil {
prefs.set(callbackHandle, forKey: CALLBACK_HANDLE)
prefs.set(callbackHandle, forKey: CALLBACK_HANDLE_ON_RESTART)
prefs?.set(callbackHandle, forKey: CALLBACK_HANDLE)
prefs?.set(callbackHandle, forKey: CALLBACK_HANDLE_ON_RESTART)
}
}

private func updateOptions(call: FlutterMethodCall) {
guard let argsDict = call.arguments as? Dictionary<String, Any> else { return }
let prefs = UserDefaults.standard
let prefs = UserDefaults(suiteName: USER_DEFAULTS_DOMAIN)

let notificationContentTitle = argsDict[NOTIFICATION_CONTENT_TITLE] as? String
?? prefs.string(forKey: NOTIFICATION_CONTENT_TITLE)
?? prefs?.string(forKey: NOTIFICATION_CONTENT_TITLE)
?? ""
let notificationContentText = argsDict[NOTIFICATION_CONTENT_TEXT] as? String
?? prefs.string(forKey: NOTIFICATION_CONTENT_TEXT)
?? prefs?.string(forKey: NOTIFICATION_CONTENT_TEXT)
?? ""
let callbackHandle = argsDict[CALLBACK_HANDLE] as? Int64

prefs.set(notificationContentTitle, forKey: NOTIFICATION_CONTENT_TITLE)
prefs.set(notificationContentText, forKey: NOTIFICATION_CONTENT_TEXT)
prefs.removeObject(forKey: CALLBACK_HANDLE)
prefs?.set(notificationContentTitle, forKey: NOTIFICATION_CONTENT_TITLE)
prefs?.set(notificationContentText, forKey: NOTIFICATION_CONTENT_TEXT)
prefs?.removeObject(forKey: CALLBACK_HANDLE)
if callbackHandle != nil {
prefs.set(callbackHandle, forKey: CALLBACK_HANDLE)
prefs.set(callbackHandle, forKey: CALLBACK_HANDLE_ON_RESTART)
prefs?.set(callbackHandle, forKey: CALLBACK_HANDLE)
prefs?.set(callbackHandle, forKey: CALLBACK_HANDLE_ON_RESTART)
}
}

private func clearOptions() {
let prefs = UserDefaults.standard
for key in prefs.dictionaryRepresentation().keys {
prefs.removeObject(forKey: key.description)
}
UserDefaults.standard.removeSuite(named: USER_DEFAULTS_DOMAIN)
}
}
2 changes: 2 additions & 0 deletions ios/Classes/service/BackgroundServicePrefsKey.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
// Created by WOO JIN HWANG on 2021/08/11.
//

let USER_DEFAULTS_DOMAIN = "flutter_foreground_task"

let NOTIFICATION_CONTENT_TITLE = "notificationContentTitle"
let NOTIFICATION_CONTENT_TEXT = "notificationContentText"
let SHOW_NOTIFICATION = "showNotification"
Expand Down