1

I am creating a react native app, which is mainly for drivers, it has this feature to notify the driver with voice commands while riding, as the driver is riding it will be difficult for him to read test notification. So am new to mobile app development and need a solution to audio when pushing notification from fire base server.

I was planning to push plain text and convert it to audio using the TTS library, i was able to do this but when the app is inactive it doesn't work.

I need a solution that should work in both ios/android which can either play an audio file or utilize TTS to convey the message to user when the phone is locked, app is running in the background. great if you can do it even when the app is not running in background.

Basilin
  • 11
  • 2

1 Answers1

0

For android you can write native code for this purpose. Create a BroadcastReceiver Create a TTS Service Once you receive a notification you can send a broadcast. From the BroadcastReceiver you can start the TTS service as an Intent.

public class TTS extends Service implements TextToSpeech.OnInitListener {

  private TextToSpeech _testToSpeach;

  @Override
  public IBinder onBind(Intent arg0) {
    return null;
  }

  @Override
  public void onCreate() {
    super.onCreate();
  }

  @Override
  public void onDestroy() {
    // TODO Auto-generated method stub
    if (_testToSpeach != null) {
        _testToSpeach.stop();
        _testToSpeach.shutdown();
    }
    super.onDestroy();
  }

  @Override
  public void onStart(Intent intent, int startId) {
    _testToSpeach = new TextToSpeech(this, this);
    speakOut();
  }

  @Override
  public void onInit(int status) {
    if (status == TextToSpeech.SUCCESS) {
      int result = _testToSpeach.setLanguage(Locale.US);
      if (result == TextToSpeech.LANG_MISSING_DATA
        || result == TextToSpeech.LANG_NOT_SUPPORTED) {
          Log.e("TTS", "Language not supported");
      }
      speakOut();
    } else {
      Log.e("TTS", "Initilization Failed!");
    }
  }

  private void speakOut() {
    _testToSpeach.speak("its working", TextToSpeech.QUEUE_FLUSH, null);
  }
}

You can Start the TTS service from BroadcastReceiver like this

context.startService(new Intent(context, TTS.class));