Home > database >  How to stream data from Broadcast Receiver to Flutter?
How to stream data from Broadcast Receiver to Flutter?

Time:07-21

I want to stream data from Broadcast Receiver (native Android) to Flutter.

The Receiver :

public class SentReceiver extends BroadcastReceiver {
 @Override
    public void onReceive(Context context, Intent intent) {
        Log.d(TAG, "onReceive: "    intent.getAction());
        int resultCode = this.getResultCode();
        boolean successfullySent = resultCode == Activity.RESULT_OK;

        if (successfullySent)
        {

            String messageId = intent.getStringExtra("messageId");
            Log.e(TAG, "run: "  messageId);
            //From here I want to stream data 
        }
    }
}

This is the code in Flutter to receive the data:

static const sentSMSChannel = EventChannel(RECEIVE_REPORT);

onStreamSent() {
    _streamSubscription
    =sentSMSChannel.receiveBroadcastStream().listen((event) {
      print(event);
    });
}

I need a way to receive the delivery or sent report from native to Flutter or a way to access the Flutter SQLite database from native Android to update the rows.

Database init in Flutter:

_database = await _initDB("ESNanoSoft.db");

And this code to init the database and open it:

Future<Database> _initDB(String filePath) async {
    final dbPath = await getDatabasesPath();
    final path = join(dbPath, filePath);

    return await openDatabase(path,
        version: dbVersion, onCreate: _createDB, onUpgrade: _onUpgrade);
}

CodePudding user response:

Create native EventChannel and setStreamHandler to get access to EventSink. Use EventSink to send events to Flutter.

See Add an Android platform-specific implementation for details. In this case EventChannel replaces MethodChannel.

  • Related