flutter_foreground_task
flutter_foreground_task copied to clipboard
launchApp method not working
launchApp을 눌렀을때 앱이 켜질줄 알았는데 안켜지내요 ㅠㅠ 앱이 켜져 있을때 특정 페이지로 이동은 하는데 켜지지는 않아요
@Kimsangwon0509
혹시 구현하신 TaskHandler 부분 볼 수 있을까요?
만약 FlutterForegroundTask.updateService 함수를 호출하여 다른 TaskHandler를 시작한다고 했을 때
그 다른 TaskHandler에도 onNotificationPressed을 구현하셔야 합니다.
class FirstTaskHandler extends TaskHandler { SendPort? _sendPort; int _eventCount = 0;
@override
Future
// You can use the getData function to get the stored data.
final customData = await FlutterForegroundTask.getData<String>(key: 'customData');
print('customData: $customData');
}
@override
Future
// Send data to the main isolate.
sendPort?.send(_eventCount);
_eventCount++;
}
@override
Future
@override void onButtonPressed(String id) { // Called when the notification button on the Android platform is pressed. print('onButtonPressed >> $id'); }
@override void onNotificationPressed() { // Called when the notification itself on the Android platform is pressed. FlutterForegroundTask.launchApp("/resume-route");
// Note that the app will only route to "/resume-route" when it is exited so
// it will usually be necessary to send a message through the send port to
// signal it to restore state when the app is already started
_sendPort?.send('onNotificationPressed');
} } 이렇게 사용중입니다.
Android 10 이상부터 Activity 시작에 제한 사항이 있어 문제가 생긴 것 같습니다.
문제 해결되면 말씀드리겠습니다.
감사합니다!!!!!
@Kimsangwon0509
Android 10 이상부터 Activity 시작에 제한 사항이 있어 SYSTEM_ALERT_WINDOW 권한을 사용하여 문제를 해결하고자 합니다. 제한 사항에 대한 자세한 내용은 공식 페이지를 참고해주세요.
dependencies:
flutter_foreground_task: ^3.7.1
AndroidMenifest.xml 파일에 아래 권한을 추가해주세요.
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
FlutterForegroundTask 시작 함수를 아래와 같이 수정하세요.
Future<bool> _startForegroundTask() async {
// "android.permission.SYSTEM_ALERT_WINDOW" permission must be granted for
// onNotificationPressed function to be called.
//
// When the notification is pressed while permission is denied,
// the onNotificationPressed function is not called and the app opens.
//
// If you do not use the onNotificationPressed or launchApp function,
// you do not need to write this code.
if (!await FlutterForegroundTask.canDrawOverlays) {
// 설정 가이드 팝업 추가
final isGranted =
await FlutterForegroundTask.openSystemAlertWindowSettings();
if (!isGranted) {
print('SYSTEM_ALERT_WINDOW permission denied!');
return false;
}
}
// You can save data using the saveData function.
await FlutterForegroundTask.saveData(key: 'customData', value: 'hello');
ReceivePort? receivePort;
if (await FlutterForegroundTask.isRunningService) {
receivePort = await FlutterForegroundTask.restartService();
} else {
receivePort = await FlutterForegroundTask.startService(
notificationTitle: 'Foreground Service is running',
notificationText: 'Tap to return to the app',
callback: startCallback,
);
}
return _registerReceivePort(receivePort);
}
SYSTEM_ALERT_WINDOW 권한이 요청되기 전에 사용자에게 설정 가이드 팝업을 띄워주면 좋을 것 같습니다. launchApp 함수를 사용하는게 아니라면 요청 코드를 제거하셔도 됩니다.
제가 보기에는 현재 방법이 최선의 방법인 것 같습니다. 더 좋은 방법이 있다면 알려주시면 감사하겠습니다.