Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Catch onNewIntent() in flutter plugin class

Question title might be a bit confusing. I will try to explain more details.

I am writing a flutter plugin to use NFC.

I have example Android Activity that triggers onNewIntent() as it receives NDEF message. At this moment the message payload is ready to be used.

class MainActivity : AppCompatActivity() {
    private var mNfcAdapter: NfcAdapter? = null
    private var mPendingIntent: PendingIntent? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        mNfcAdapter = NfcAdapter.getDefaultAdapter(this)

        mPendingIntent = PendingIntent.getActivity(
                this, 0,
                Intent(this, this.javaClass)
                    .addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0
            )
    }

    override fun onResume() {
        super.onResume()
        mNfcAdapter?.enableForegroundDispatch(this, mPendingIntent, null, null)
    }

    override fun onPause() {
        super.onPause()
        mNfcAdapter?.disableForegroundDispatch(this)
    }

    override fun onNewIntent(intent: Intent) {
        super.onNewIntent(intent)
        if (NfcAdapter.ACTION_NDEF_DISCOVERED == intent.action) {
            intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES)?.also { rawMessages ->
                val messages: List<NdefMessage> = rawMessages.map { it as NdefMessage }

                parseNDEFMessage(messages)
            }
        }
    }

    private fun parseNDEFMessage(messages: List<NdefMessage>) {
        //do parsing and display payload
    }

I need to have same functionality in plugin class.

Here is my plugin class

class NfcforflutterPlugin(private var activity: Activity?) : MethodCallHandler, EventChannel.StreamHandler {
  private var mNfcAdapter: NfcAdapter? = null
  private var mPendingIntent: PendingIntent? = null
    private var  mEventSink: EventChannel.EventSink? = null

  companion object {
    @JvmStatic
    fun registerWith(registrar: Registrar) {
        var nfcforflutterPlugin = NfcforflutterPlugin(registrar.activity())
        val channel = MethodChannel(registrar.messenger(), "nfcforflutter")
        channel.setMethodCallHandler(nfcforflutterPlugin)
        val eventChannel = EventChannel(registrar.messenger(), "nfcforflutter")
        eventChannel.setStreamHandler(nfcforflutterPlugin)
    }
  }

  fun getNDEFMessage(): String{
    return "CALLED getNDEFMessage"
  }

  fun initializeNFCReading():Boolean {
    mNfcAdapter = NfcAdapter.getDefaultAdapter(activity)

    if(!checkNFCEnable())
            return false
    val intent = Intent(activity?.applicationContext, activity?.javaClass)
    intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP)

    val pendingIntent = PendingIntent.getActivity(activity?.applicationContext, 0, intent, 0)

    mNfcAdapter?.enableForegroundDispatch(activity, pendingIntent, null, null)
        return true
  }

  private fun checkNFCEnable(): Boolean {
    return if (mNfcAdapter == null) {
      false
    } else {
      mNfcAdapter!!.isEnabled
    }
  }

  override fun onMethodCall(call: MethodCall, result: Result) {
    if (call.method == "getPlatformVersion") {
      result.success("Android ${android.os.Build.VERSION.RELEASE}")
      return
    }
    if (call.method == "getNDEFMessage") {
      result.success(getNDEFMessage())
      return
    }
      if (call.method == "initializeNFCReading") {
          result.success(initializeNFCReading())
          return
      }

    result.notImplemented()

  }

  override fun onListen(p0: Any?, eventSink: EventChannel.EventSink?) {
        mEventSink = eventSink
  }

  override fun onCancel(p0: Any?) {
  }

SInce NfcforflutterPlugin is not an Activity, I need a triggering event in NfcforflutterPlugin class as onNewIntent() in Android Activity which will be invoked at the message discovering moment, so that I can run mEventSink.success(ndefMessage) and pass message to Flutter side.

Any idea?

like image 857
drek Avatar asked Dec 28 '25 14:12

drek


1 Answers

Implement PluginRegistry.NewIntentListener for listening intent methods. Check here.

It has onNewIntent method. you can use it. Like,

class NfcforflutterPlugin(private var activity: Activity?) : MethodCallHandler, EventChannel.StreamHandler, PluginRegistry.NewIntentListener {
      ....................
      ....................
      override fun onNewIntent(intent:Intent):Boolean  {
            //handle data with intent
        return false;
     }
}
like image 132
Abhay Koradiya Avatar answered Dec 31 '25 18:12

Abhay Koradiya



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!