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

2,045

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.

Share:
2,045
Admin
Author by

Admin

Updated on December 15, 2022

Comments

  • Admin
    Admin over 1 year

    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.