flutter_foreground_task icon indicating copy to clipboard operation
flutter_foreground_task copied to clipboard

launchApp method not working

Open Kimsangwon0509 opened this issue 3 years ago • 6 comments

launchApp을 눌렀을때 앱이 켜질줄 알았는데 안켜지내요 ㅠㅠ 앱이 켜져 있을때 특정 페이지로 이동은 하는데 켜지지는 않아요

Kimsangwon0509 avatar Apr 29 '22 07:04 Kimsangwon0509

@Kimsangwon0509

혹시 구현하신 TaskHandler 부분 볼 수 있을까요?

Dev-hwang avatar Apr 29 '22 07:04 Dev-hwang

만약 FlutterForegroundTask.updateService 함수를 호출하여 다른 TaskHandler를 시작한다고 했을 때

그 다른 TaskHandler에도 onNotificationPressed을 구현하셔야 합니다.

Dev-hwang avatar Apr 29 '22 07:04 Dev-hwang

class FirstTaskHandler extends TaskHandler { SendPort? _sendPort; int _eventCount = 0;

@override Future onStart(DateTime timestamp, SendPort? sendPort) async { _sendPort = sendPort;

// You can use the getData function to get the stored data.
final customData = await FlutterForegroundTask.getData<String>(key: 'customData');
print('customData: $customData');

}

@override Future onEvent(DateTime timestamp, SendPort? sendPort) async { FlutterForegroundTask.updateService( notificationTitle: 'FirstTaskHandler', notificationText: timestamp.toString(), //callback: _eventCount >= 10 ? updateCallback : null, );

// Send data to the main isolate.
sendPort?.send(_eventCount);

_eventCount++;

}

@override Future onDestroy(DateTime timestamp, SendPort? sendPort) async { // You can use the clearAllData function to clear all the stored data. await FlutterForegroundTask.clearAllData(); }

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

} } 이렇게 사용중입니다.

Kimsangwon0509 avatar Apr 29 '22 08:04 Kimsangwon0509

Android 10 이상부터 Activity 시작에 제한 사항이 있어 문제가 생긴 것 같습니다.

문제 해결되면 말씀드리겠습니다.

Dev-hwang avatar Apr 29 '22 08:04 Dev-hwang

감사합니다!!!!!

Kimsangwon0509 avatar Apr 29 '22 08:04 Kimsangwon0509

@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 함수를 사용하는게 아니라면 요청 코드를 제거하셔도 됩니다.

제가 보기에는 현재 방법이 최선의 방법인 것 같습니다. 더 좋은 방법이 있다면 알려주시면 감사하겠습니다.

Dev-hwang avatar May 02 '22 06:05 Dev-hwang