Flutter FCM topic messages from multiple firebase projects

Viewed 1074

Is there a way in Flutter to receive topic notifications from multiple firebase projects, i have been looking for a way but no luck, Flutter plugin do not seem to have even the generated token by sender id method, i am also looking for a way to implement it using rest api but have no idea where to start.any directions are welcome.

2 Answers

Firebase Cloud Functions should help you out here. Cloud Functions allow you to perform database, auth, cloud messaging and other functions on the server side. You can crate a web app on fiebase console and call the functions using https. Here is link to a tutorial I found very useful:

https://www.youtube.com/playlist?list=PL4cUxeGkcC9i_aLkr62adUTJi53y7OjOf

Its true, the firebase dependence to flutter don't have support to this functionality, but you can create methodChannel to call native code from flutter

First, add MethodChannel in anywhere

static const platform = const MethodChannel('flutter.native/helper'); //<-- This string argument is custom, but must the same in java code
Future<void> responseFromNativeCode() async {
  String response = "";
  try {
    String senderID = '52723600452'; // <----- this is the identifier of another project
    final String result = await platform.invokeMethod(
      'onNewToken', <String, dynamic>{'senderId': senderID});
    response = result;
    print(response); // <----- this is the token, with this, you can receive notifications from another firebase project
  } on PlatformException catch (e) {
    response = "Failed to Invoke: '${e.message}'.";
    print(response);
  }
}

This solution only works in android, but, you need implements to ios too... swift

Now... you need implement the methodChannel in android and add firebase implementation... here is a example

public class MainActivity extends FlutterActivity {
  private String CHANNEL = "flutter.native/helper";//This string must be same in flutter declaration
  private String token = "";

  @Override
  public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
    super.configureFlutterEngine(flutterEngine);

    new MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), CHANNEL)
                .setMethodCallHandler((call, result) -> {
                    if(call.method.equals("onNewToken")){//This string must be same too in flutter declaration
                        String senderId = call.argument("senderId");
                        TokenOtherProject a = new TokenOtherProject();
                        AsyncTask b = a.execute(senderId);
                        try {
                            b.get();//This is like await in flutter
                            result.success(token);//with this, you send the token to flutter project... and handler in page or idk
                        } catch (ExecutionException e) {
                            e.printStackTrace();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                });
}

private class TokenOtherProject extends AsyncTask<String, Void, Void> {
    @Override
    protected Void doInBackground(String... params) {
        try {
            token = FirebaseInstanceId.getInstance().getToken(params[0],
                    FirebaseMessaging.INSTANCE_ID_SCOPE);
        } catch (IOException e) {
            e.printStackTrace();
        }
        new Handler(Looper.getMainLooper()).post(new Runnable(){
            @Override
            public void run() {
                Log.d("*TokenNuevo: " , token + " ");
            }
        });
        return null;
    }
}}

You can get more information to how you can communicate with native... flutter fcm package must have this functionality but no :/

Anyway ... good luck

Related