Skip to content

Commit

Permalink
Add flutter code for usage together with firebase_messaging
Browse files Browse the repository at this point in the history
  • Loading branch information
gbhrdt committed Sep 16, 2024
1 parent 51c38f7 commit ac2c19c
Showing 1 changed file with 94 additions and 0 deletions.
94 changes: 94 additions & 0 deletions docs/sdks/flutter/troubleshooting.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<application>` tag:
```
<service
android:name=".FirebaseMessagingServiceProxy"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
``
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<FirebaseMessagingService> 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<FirebaseMessagingService> 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<T> {
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. [...]
Expand Down

0 comments on commit ac2c19c

Please sign in to comment.