From ac2c19c60ed539845eb09fe2c03661c7cbd5a6e8 Mon Sep 17 00:00:00 2001 From: gbhrdt Date: Mon, 16 Sep 2024 10:53:55 +0200 Subject: [PATCH] Add flutter code for usage together with firebase_messaging --- docs/sdks/flutter/troubleshooting.md | 94 ++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) diff --git a/docs/sdks/flutter/troubleshooting.md b/docs/sdks/flutter/troubleshooting.md index 8ad95517..b682810b 100644 --- a/docs/sdks/flutter/troubleshooting.md +++ b/docs/sdks/flutter/troubleshooting.md @@ -7,6 +7,100 @@ title: Troubleshooting Please make sure you are not using the `firebase_messaging` dependency together with our SDK. The Android Firebase setup should be done directly in the Android project by following the official guide from the Firebase docs. +If you want to use our SDK together with `firebase_messaging`, although we do not recommend it, you can use the following workaround: + +1. Add this to your `android/app/src/main/AndroidManifest.xml` file inside the `` tag: +``` + + + + + +`` + +2. Create the `FirebaseMessagingServiceProxy` class in your Android project: +``` +package com.example; + +import androidx.annotation.NonNull; + +import com.cleverpush.service.CleverPushFcmListenerService; +import io.flutter.plugins.firebase.messaging.FlutterFirebaseMessagingService; +import com.google.firebase.messaging.FirebaseMessagingService; +import com.google.firebase.messaging.RemoteMessage; + +import java.lang.reflect.Field; +import java.util.ArrayList; +import java.util.List; + +public class FirebaseMessagingServiceProxy extends FirebaseMessagingService { + private final List messagingServices = new ArrayList<>(2); + + public FirebaseMessagingServiceProxy() { + messagingServices.add(new CleverPushFcmListenerService()); + messagingServices.add(new FlutterFirebaseMessagingService()); + } + + @Override + public void onNewToken(@NonNull String token) { + delegate(service -> { + injectContext(service); + service.onNewToken(token); + }); + } + + @Override + public void onMessageReceived(@NonNull RemoteMessage remoteMessage) { + delegate(service -> { + injectContext(service); + service.onMessageReceived(remoteMessage); + }); + } + + private void delegate(CPAction action) { + for (FirebaseMessagingService service : messagingServices) { + action.run(service); + } + } + + private void injectContext(FirebaseMessagingService service) { + setField(service, "mBase", this); + } + + private boolean setField(Object targetObject, String fieldName, Object fieldValue) { + Field field; + try { + field = targetObject.getClass().getDeclaredField(fieldName); + } catch (NoSuchFieldException e) { + field = null; + } + Class superClass = targetObject.getClass().getSuperclass(); + while (field == null && superClass != null) { + try { + field = superClass.getDeclaredField(fieldName); + } catch (NoSuchFieldException e) { + superClass = superClass.getSuperclass(); + } + } + if (field == null) { + return false; + } + field.setAccessible(true); + try { + field.set(targetObject, fieldValue); + return true; + } catch (IllegalAccessException e) { + return false; + } + } + + interface CPAction { + void run(T t); + } +} +``` ## iOS: [...] does not contain bitcode. You must rebuild it with bitcode enabled (Xcode setting ENABLE_BITCODE), obtain an updated library from the vendor, or disable bitcode for this target. [...]