okadan / flutter-nfc-manager

A Flutter plugin for accessing the NFC features on Android and iOS.
https://pub.dev/packages/nfc_manager
MIT License
207 stars 134 forks source link

How to open the app when nfc is detected #66

Closed dfourcfive closed 2 years ago

dfourcfive commented 2 years ago

I am trying to open a certain screen when the nfc is detected , i have tried the workmanager but it didn't work , after reaserch i found this artice but i'm not sure how to use it for my own purpose https://muetsch.io/how-to-receive-sharing-intents-in-flutter.html

tonyCruzNagy commented 2 years ago

you can see this video video youtube

dfourcfive commented 2 years ago

@rus0n thank you i have did a similiar approche
in the native code i check if the app have been open by nfc intent and i save the content in shared Preferences and in the dart/flutter side i check if there's data saved or not

tonyCruzNagy commented 2 years ago

Now, I'm trying to get data when the app open cause NFC tag. I think that I have to use intent filter and use methodhadler in native code to send the data to flutter. Do you implement it and works fine?

dfourcfive commented 2 years ago

yes i'm using this code in the onresume method

if ((NfcAdapter.ACTION_NDEF_DISCOVERED == intent.action)||(NfcAdapter.ACTION_TAG_DISCOVERED == intent.action)||(NfcAdapter.ACTION_TECH_DISCOVERED == intent.action)) {
                        intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES)?.also { rawMessages ->
                            val messages: List<NdefMessage> = rawMessages.map { it as NdefMessage }
                            if(messages.first() != null && messages.first().records != null){
                                val uriRecord = String(messages.first().records.first().payload);
                                //MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL).invokeMethod("nfc_native",uriRecord);
                                this.code=uriRecord;
                                var preferences = context.getSharedPreferences("FlutterSharedPreferences", Context.MODE_PRIVATE)
                                preferences.edit().putString("flutter.nfcMessage",this.code).apply();

                            }

                        }
                    }   

and in the dart side i just check the value in sharedPreferences if its exist and use it (i remove it after using it so i don't have to handle it more than once) ps : in the dart/flutter side the key of data in sharedPrefrences is only "nfcMessage"