Coder Social home page Coder Social logo

Comments (13)

kakajansh avatar kakajansh commented on June 1, 2024 1

@shounakmulay they are about sending SMS in background. We also encounter this problem. We have firebase configured, and when receive background notification we want to send SMS.

final FirebaseMessaging _firebaseMessaging = FirebaseMessaging();
final Telephony telephony = Telephony.instance;

@override
void initState() {
  initFirebase();
  super.initState();
}

initFirebase() async {
  await telephony.requestPhoneAndSmsPermissions;
  _firebaseMessaging.requestNotificationPermissions();
  _firebaseMessaging.subscribeToTopic('verify');
  _firebaseMessaging.configure(
    onBackgroundMessage: _onBackgroundMessage,
    onMessage: (Map<String, dynamic> notification) async {
      String phone = notification['data']['phone'];
      String message = notification['data']['body'];

      telephony.sendSms(to: phone, message: message);
    },
  );
}

onMessage fires only when app is open, and it works. But on onBackgroundMessage it throws above MissingPluginException error.

Future<dynamic> _onBackgroundMessage(
  Map<String, dynamic> notification,
) async {
  final String phone = notification['data']['phone'];
  final String message = notification['data']['body'];

  Telephony.instance.sendSms(to: phone, message: message);

  return Future<void>.value();
}

from telephony.

shounakmulay avatar shounakmulay commented on June 1, 2024 1

@kakajansh Thanks for the explanation.
I guess I had an incorrect understand of the problem.
Will take a look and update here.

from telephony.

dinahapuque avatar dinahapuque commented on June 1, 2024

I'm also getting this exception when using this package with WorkManager and AndroidAlarmManager ):

from telephony.

shounakmulay avatar shounakmulay commented on June 1, 2024

Hey @cuongdl @dinahapuque

I was not able to reproduce this issue.
I could correctly receive SMS in background on an emulator as well as on a physical device.
Could you please provide a reproducible example that I can look into?

from telephony.

dinahapuque avatar dinahapuque commented on June 1, 2024

Using AndroidAlertManager causes the same error:

(MissingPluginException(No implementation found for method sendSms on channel plugins.shounakmulay.com/foreground_sms_channel))

E.g:

void main() async {
  WidgetsFlutterBinding.ensureInitialized();

  final int helloAlarmID = 0;
  await AndroidAlarmManager.initialize();

  runApp(App());

  await AndroidAlarmManager.periodic(
    const Duration(minutes: 1),
    helloAlarmID,
    sendSms,
    wakeup: true,
  );
}

Future<void> sendSms() async {
  try {
    Telephony.instance.sendSms(
        to: myPhone,
        message: myMessage;
  } catch (e) {
    print(e);
  }
}

from telephony.

shivanshPurple avatar shivanshPurple commented on June 1, 2024

Any updates!? I am also having the same problem when I try to send a SMS in the background by an android service

from telephony.

tareqfaris avatar tareqfaris commented on June 1, 2024

I also have the same problem, is there a solution?

from telephony.

shounakmulay avatar shounakmulay commented on June 1, 2024

I think the problem is that when the app is in background, the plugin initialization does not happen correctly on the android side.
Also if you app is still active in background, call the plugin from the background makes it loose the reference to the original method channel that is used as a communication bridge.

Maybe adding a backgroundInstance to call telephony methods from the background could be the solution. This way we don't make the original instance inactive and you can still continue using it once the app comes back to the foreground.

from telephony.

shounakmulay avatar shounakmulay commented on June 1, 2024

This is fixed in v0.1.1.

Checkout https://github.com/shounakmulay/Telephony/tree/v0.1.1#executing-in-background

from telephony.

kakajansh avatar kakajansh commented on June 1, 2024

@shounakmulay For me it doesn't work with firebase background handler. But works when receiving sms in background.

This one works

telephony.listenIncomingSms(
  onNewMessage: (SmsMessage message) { ... },
  onBackgroundMessage: backgroundMessageHandler,
);

backgroundMessageHandler(SmsMessage message) async {
  Telephony.backgroundInstance.sendSms(to: "123456789", message: "Message from background");
}

This one throws Unhandled Exception: MissingPluginException(No implementation found for method sendSms on channel plugins.shounakmulay.com/foreground_sms_channel)

// foreground
FirebaseMessaging.onMessage.listen((RemoteMessage notification) async {
      ...
      Telephony.instance.sendSms(to: phone, message: msg);
});

// background
FirebaseMessaging.onBackgroundMessage(onBackgroundMessage);

Future<void> _onBackgroundMessage(RemoteMessage message) async {
  final String phone = message.data['phone'];
  final String body = message.data['body'];

  Telephony.backgroundInstance.sendSms(to: phone, message: body);
}

Could you please direct what is missing here. There are some other issues maybe related to this about mixing plugins.
How to make Flutter Workmanager plugin and Location plugin work together

from telephony.

shounakmulay avatar shounakmulay commented on June 1, 2024

@kakajansh will take a look.
Would be great if you could give me some more info like does this happen only with the firebase messaging plugin or does it happen with other plugins as well?
Thanks.

from telephony.

shounakmulay avatar shounakmulay commented on June 1, 2024

Closing this for now. Feel free to reopen with any updates.

from telephony.

sharifka avatar sharifka commented on June 1, 2024

i have an error about using backgroundMessageHandler
Telephony.backgroundInstance.dialPhoneNumber('12345');
Telephony.instance.dialPhoneNumber('1234');
not works,but when used foreground is working fine.
what is the error,please any can helpe to fix this error.

from telephony.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.