I'm trying to send a notification from dart code, when incoming call is being received. I couldn't manage to make a bridge between android native code and flutter code. The thing is that I need this feature to work in the background. I would describe the cycle like this:
- Flutter android native code has to detect call in a background
- Native code has to call a certain class from Flutter dart code
- This class finds certain information in database about the caller
- after finding it, it has to pop up a notification with certain details.
I managed to do part 3 and 4 but I need help with part 1 and 2.
I tried to figure out this given example, but it was to complicated
I found this method to detect the incoming call, but I couldn't attach it to mainActivity class and how do part 2
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.PhoneStateListener;
import android.widget.Toast;
import android.telephony.TelephonyManager;
public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(final Context context, Intent intent) {
TelephonyManager telephony = (TelephonyManager)context.getSystemService(Service.TELEPHONY_SERVICE);
telephony.listen(new PhoneStateListener(){
@Override
public void onCallStateChanged(int state, String incomingNumber) {
// I think this is the part, where I should call my dart class.
}
},PhoneStateListener.LISTEN_CALL_STATE);
}
}
The question is how to properly detect a call and how to call a dart class from android native code?