|
| 1 | +// Copyright 2019 The Chromium Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | +package io.flutter.plugins.firebase.inappmessaging |
| 5 | + |
| 6 | +import com.google.android.gms.tasks.Task |
| 7 | +import com.google.android.gms.tasks.TaskCompletionSource |
| 8 | +import com.google.firebase.FirebaseApp |
| 9 | +import com.google.firebase.inappmessaging.FirebaseInAppMessaging |
| 10 | +import io.flutter.embedding.engine.plugins.FlutterPlugin |
| 11 | +import io.flutter.embedding.engine.plugins.FlutterPlugin.FlutterPluginBinding |
| 12 | +import io.flutter.plugin.common.MethodCall |
| 13 | +import io.flutter.plugin.common.MethodChannel |
| 14 | +import io.flutter.plugin.common.MethodChannel.MethodCallHandler |
| 15 | +import io.flutter.plugin.common.MethodChannel.Result |
| 16 | +import io.flutter.plugins.firebase.core.FlutterFirebasePlugin |
| 17 | + |
| 18 | +/** FirebaseInAppMessagingPlugin */ |
| 19 | +class FirebaseInAppMessagingPlugin : FlutterFirebasePlugin, FlutterPlugin, MethodCallHandler { |
| 20 | + private var channel: MethodChannel? = null |
| 21 | + |
| 22 | + override fun onAttachedToEngine(binding: FlutterPluginBinding) { |
| 23 | + channel = |
| 24 | + MethodChannel(binding.binaryMessenger, METHOD_CHANNEL_NAME).also { |
| 25 | + it.setMethodCallHandler(this) |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + override fun onDetachedFromEngine(binding: FlutterPluginBinding) { |
| 30 | + channel?.setMethodCallHandler(null) |
| 31 | + channel = null |
| 32 | + } |
| 33 | + |
| 34 | + override fun onMethodCall(call: MethodCall, result: Result) { |
| 35 | + when (call.method) { |
| 36 | + "FirebaseInAppMessaging#triggerEvent" -> { |
| 37 | + val eventName = requireNotNull(call.argument<String>("eventName")) |
| 38 | + FirebaseInAppMessaging.getInstance().triggerEvent(eventName) |
| 39 | + result.success(null) |
| 40 | + } |
| 41 | + "FirebaseInAppMessaging#setMessagesSuppressed" -> { |
| 42 | + val suppress = requireNotNull(call.argument<Boolean>("suppress")) |
| 43 | + FirebaseInAppMessaging.getInstance().setMessagesSuppressed(suppress) |
| 44 | + result.success(null) |
| 45 | + } |
| 46 | + "FirebaseInAppMessaging#setAutomaticDataCollectionEnabled" -> { |
| 47 | + val enabled = call.argument<Boolean>("enabled") |
| 48 | + FirebaseInAppMessaging.getInstance().setAutomaticDataCollectionEnabled(enabled) |
| 49 | + result.success(null) |
| 50 | + } |
| 51 | + else -> result.notImplemented() |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + override fun getPluginConstantsForFirebaseApp(firebaseApp: FirebaseApp): Task<Map<String, Any>?> { |
| 56 | + val taskCompletionSource = TaskCompletionSource<Map<String, Any>?>() |
| 57 | + |
| 58 | + FlutterFirebasePlugin.cachedThreadPool.execute { |
| 59 | + try { |
| 60 | + taskCompletionSource.setResult(null) |
| 61 | + } catch (exception: Exception) { |
| 62 | + taskCompletionSource.setException(exception) |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + return taskCompletionSource.task |
| 67 | + } |
| 68 | + |
| 69 | + override fun didReinitializeFirebaseCore(): Task<Void> { |
| 70 | + val taskCompletionSource = TaskCompletionSource<Void>() |
| 71 | + |
| 72 | + FlutterFirebasePlugin.cachedThreadPool.execute { |
| 73 | + try { |
| 74 | + taskCompletionSource.setResult(null) |
| 75 | + } catch (exception: Exception) { |
| 76 | + taskCompletionSource.setException(exception) |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + return taskCompletionSource.task |
| 81 | + } |
| 82 | + |
| 83 | + companion object { |
| 84 | + private const val METHOD_CHANNEL_NAME = "plugins.flutter.io/firebase_in_app_messaging" |
| 85 | + } |
| 86 | +} |
0 commit comments