# Flutter

## Android

{% hint style="info" %}
본 가이드는 Flutter 3.29.0, Dart 3.7.0 기준으로 작성되었습니다.

SDK가 지원하는 최소 버전은 Android API 21 이상입니다.
{% endhint %}

### 플러그인 다운로드

1\) 플러그인을 다운로드 합니다.

[**▶다운로드**](https://developers.fingerpush.com/app-push/sdk-down#flutter)

2\) 프로젝트 폴더와 같은 경로에 압축을 해제합니다.

### 플러그인 적용하기

1\) 프로젝트 pubspec.yaml 에 'fingerpush\_plugin' 을 추가합니다.&#x20;

{% hint style="danger" %} <mark style="color:red;">fingerpush\_plugin 폴더를 상대 경로로 참조하여 추가합니다.</mark>
{% endhint %}

{% code title="<project>/pubspec.yaml" %}

```
fingerpush_plugin:
    path: ../fingerpush_plugin/
```

{% endcode %}

2\) 명령어를 실행하여 플러그인을 적용합니다.&#x20;

```
flutter pub get
```

### 프로젝트 설정하기

1\) 핑거푸시 앱 생성 및 FCM 프로젝트 생성은 Android 매뉴얼을 참고바랍니다.

{% content-ref url="android" %}
[android](https://developers.fingerpush.com/app-push/sdk-manual/android)
{% endcontent-ref %}

2\) gradle 설정

a. 프로젝트 레벨의 settings.gradle.kts 에 'com.google.gms:google-services' 플러그인을 추가합니다.

{% code title="<project>/settings.gradle.kts" %}

```gradle
plugins {
    ...
    id("com.google.gms.google-services") version "4.4.3"
}
```

{% endcode %}

b. 앱 레벨의 build.gradle.kts 하단에 google-services 플러그인을 추가합니다.

{% code title="<project>/<app-module>/build.gradle.kts" %}

```gradle
plugins {
    id 'com.google.gms.google-services'
}
```

{% endcode %}

3\) AndroidManifest 설정

a. allowBackup 값을 false 로 설정합니다.

{% code title="AndroidManifest.xml" %}

```
<application
    ...
    android:allowBackup="false"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name">
    ...
</application>
```

{% endcode %}

b. 푸시 수신을 서비스를 추가합니다.

{% code title="AndroidManifest.xml" %}

```
<application>
    ...
    <service
        android:name="com.fingerpush.fingerpush_plugin.IntentService"
        android:exported="false">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>
    </service>
</application>
```

{% endcode %}

4\) MainActivity 설정

{% hint style="info" %}
인앱푸시를 사용하는 경우 FlutterFragmentActivity 를 상속받습니다.
{% endhint %}

{% code title="MainActivity.kt" %}

```kotlin
...
class MainActivity : FlutterFragmentActivity(), MethodCallHandler {

    private lateinit var channel: MethodChannel

    override fun onNewIntent(intent: Intent) {
        super.onNewIntent(intent)
        checkPayload(intent.extras)
    }

    // 푸시 수신 데이터 체크
    private fun checkPayload(data: Bundle?) {
        if (data != null) {
            val jsonObject = JSONObject()
            val payload = data.getBundle("payload")

            val keys = payload?.keySet()
            if (keys != null) {
                for (key in keys) {
                    jsonObject.put(key, payload?.getString(key))
                }
                sendPayload(jsonObject.toString())
            }
        }
    }
    
    private fun sendPayload(payload: String) {
        if (this::channel.isInitialized) {
            channel.invokeMethod("onNotification", payload)
        } else {
            Handler(mainLooper).postDelayed({ sendPayload(payload) }, 200)
        }
    }
    
    override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
        super.configureFlutterEngine(flutterEngine)
        GeneratedPluginRegistrant.registerWith(flutterEngine)
        channel = MethodChannel(flutterEngine.dartExecutor, "FingerPushOnNotification")
        channel.setMethodCallHandler(this@MainActivity)

        if (intent.extras != null) {
            checkPayload(intent.extras)
        }
    }

    override fun onMethodCall(call: MethodCall, result: MethodChannel.Result) {
        if (call.method.equals("onNotification")) {
            checkPayload(intent.extras)
        }
    }

}
```

{% endcode %}

### Android API Reference

Android API 는 아래 링크에서 확인 할 수 있습니다.

{% content-ref url="../api-reference/android" %}
[android](https://developers.fingerpush.com/app-push/api-reference/android)
{% endcontent-ref %}

## iOS

{% hint style="info" %}
본 가이드는 Flutter SDK 3.0.0 기준으로 작성되었습니다.

최소 버전 iOS 10 이상
{% endhint %}

### 플러그인 다운로드

1\) 플러그인을 다운로드 합니다.

[**▶다운로드**](https://developers.fingerpush.com/app-push/sdk-down#flutter)

2\) 프로젝트 폴더와 같은 경로에 압축을 해제합니다.

### 플러그인 적용하기

1\) 프로젝트 pubspec.yaml 에 'fingerpush\_plugin' 을 추가합니다.&#x20;

{% hint style="danger" %} <mark style="color:red;">fingerpush\_plugin 폴더를 상대 경로로 참조하여 추가합니다.</mark>
{% endhint %}

{% code title="<project>/pubspec.yaml" %}

```
fingerpush_plugin:
    path: ../fingerpush_plugin/
```

{% endcode %}

2\) 명령어를 실행하여 플러그인을 적용합니다.&#x20;

```
flutter pub get
```

3\) 프로젝트에 'fingerpush\_plugin.dart' import 합니다.

```objectivec
import 'package:fingerpush_plugin/fingerpush_plugin.dart';
```

4\) fingerpush\_plugin/example/lib/main.dart 파일을 참고하여 플러그인을 적용합니다.

{% hint style="danger" %} <mark style="color:red;">푸시 테스트시에는 반드시 실제 기기를 사용해주세요.</mark>

<mark style="color:red;">시뮬레이터에서는 핑거푸시를 통해 푸시 수신 받을 수 없습니다.</mark>
{% endhint %}

### 프로젝트 설정하기

1\) Xcode 로 Bundle Identifier 와 Provisioning 변경해주세요.

2\) Capability 에 Background Mode 와 Push Notifications 를 추가해주세요.

![](https://1606198054-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Ff5XCUiUhwc0T57hW21TH%2Fuploads%2FWSbk7lmTt2vwSy0hKp6F%2Fflutter_for_iOS_04-768x516.png?alt=media\&token=76fd6428-325d-4184-911a-d1beba982c8e)

### iOS API Refeence

iOS API 는 아래 링크에서 확인 할 수 있습니다.

{% content-ref url="../api-reference/ios" %}
[ios](https://developers.fingerpush.com/app-push/api-reference/ios)
{% endcontent-ref %}

## 예제 파일 적용

{% hint style="info" %}
예제 파일은 'fingerpush\_plugin > example > lib > main.dart' 에서 확인할 수 있습니다&#x20;
{% endhint %}

<figure><img src="https://1606198054-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Ff5XCUiUhwc0T57hW21TH%2Fuploads%2Fwx8syeNnOStP0qZXgAza%2FFlutter_example_path_image.png?alt=media&#x26;token=df4a4b66-acad-4866-a11a-22600aca78d7" alt=""><figcaption></figcaption></figure>
