I want to pop a notification (and upon clicking open the app) when some specfic event happens on the phone (in my case receiving a SMS with a predetermined format or from a specific phone number). Currently I use flutter SMS package and build an instance of SmsReceiver class in the main() as below and try to print the sms body to console:
void main() {
SmsReceiver receiver = new SmsReceiver();
receiver.onSmsReceived.listen((SmsMessage msg) => print(msg.body));
runApp(MyApp());
}
I test the code on a real device (Samsung Galaxy 8+). When app is running or in the background I get the sms body with no problem. However, as I close the app and receive a sms, following error? is printed in console:
W/FlutterJNI(23977): Tried to send a platform message to Flutter, but FlutterJNI was detached from native C++. Could not send. Channel: plugins.babariviere.com/recvSMS. Response ID: 0
Can you help me with this? Is it possible to listen for such events and trigger a notification even when the app is closed?
p.s. I looked into flutter local notifications. However, it looks like that this package only Schedules notifications to appear. In my case I want notifications to be event driven.
Local Notifications In Flutter 1 Setup. To allow our application to use local notifications, we need to add the flutter_local_notifications package to our project. 2 Integration. ... 3 Use Cases - Showing A Notification. ... 4 Use Cases - Scheduling A Notification. ... 5 Canceling A Notification
This example demonstrate about How to Send a notification when the Android app is closed Step 1− Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2− Add the following code to res/layout/activity_main.xml.
As the registration process is platform-specific, we are going to register our app for the Android platform. After clicking on the Android icon, we will be directed to an interface asking for the Android package name. In order to add the package name of our Flutter project, we need to locate it first.
The onResume function triggers when we receive the notification alert in the device notification bar and opens the app through the push notification itself. In this case, the app can be running in the background or not running at all. Now we are all equipped with the Flutter app.
I am facing 100% same problem. I am still looking for solution with stable performance but no luck yet, but i am writing this answer to share my experience and findings hoping that we may find a solution together.
SmsReceiver receiver = new SmsReceiver();
receiver.onSmsReceived.listen((SmsMessage msg){
// Do something to pop a notification and launch main application
});
therefore we have to find a way to run android background service through flutter app, this can be accomplished by using a flutter package to run services in background.
I am new to App development of any kind, I may be wrong but this is what i figured out yet. Please share if you find a way out. Good Luck.
You can use Telephony package and simply use the backgrounMessageHandler class for more informations Visit https://pub.dev/packages/telephony
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With