Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

flutter: pop a notification by receiving SMS when the app is closed

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.

like image 775
Hadi Avatar asked Nov 30 '19 14:11

Hadi


People also ask

How to use local notifications in flutter?

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

How to send a notification when the Android app is closed?

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.

How to register an Android app in flutter?

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.

What is onresume function in flutter?

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.


2 Answers

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.

  1. SMS reception is OK with this package.
  2. When application is closed, no app (even native Android or iOS) can respond to any event.
  3. In order to respond to text message while app is closed, native android apps runs some service. This service is just like an app but without a user interface in foreground. A service has capability of running in background even when app is closed.
  4. In flutter we have to find a way to run a background service which will listen for incoming SMS events. i.e. service will have this code at least

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.

like image 51
Adnan Ahmed Avatar answered Oct 16 '22 20:10

Adnan Ahmed


You can use Telephony package and simply use the backgrounMessageHandler class for more informations Visit https://pub.dev/packages/telephony

like image 30
ebrahim EH Avatar answered Oct 16 '22 22:10

ebrahim EH