0

I would like some insight on how to improve my current setup. The application begins with FCM message Reading and prompting the user to say a voice command.

I am not receiving any errors, sometimes it works, but not consistently. In other words, sometimes the program will speak and sometimes it will not. What do you suggest I do?

simply have the following:

public class MyFirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService {
private static final String TAG = "FirebaseMsgService";
TextToSpeech instanceTTS =null;
Context context;
String moveactivity;

Intent i;
SpeechRecognizer mRecognizer;
Intent Activity_intent;
RecognitionListener listener;




// [START receive_message]
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {

   // new OperationTTS().execute(remoteMessage.getData().get("TTS"));

    instanceTTS =new TextToSpeech(getApplicationContext(), new SdnaOnInitListener(remoteMessage.getData().get("TTS")));
    instanceTTS.setOnUtteranceProgressListener(new UtteranceProgressListener() {
        @Override
        public void onStart(String s) {

        }

        @Override
        public void onDone(String s) {
            get_voice();
            instanceTTS.shutdown();
            instanceTTS = null;
        }

        @Override
        public void onError(String s) {

        }
    });
    //추가한것
    sendNotification(remoteMessage);
}

private void sendNotification(RemoteMessage remoteMessage) {
    String message = remoteMessage.getData().get("message");
    String title = remoteMessage.getData().get("title");
    moveactivity = remoteMessage.getData().get("activity");

    Activity_intent = SetActivity(moveactivity);//new Intent(this, MenuActivity.class);
    Activity_intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, Activity_intent,
            PendingIntent.FLAG_ONE_SHOT);

    Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle(title)
            .setContentText(message)
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);

    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    PowerManager pm = (PowerManager) this.getSystemService(Context.POWER_SERVICE);
    PowerManager.WakeLock wakelock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "TAG"); //화면 깨움
    wakelock.acquire(5000);

    notificationManager.notify(1231231 /* ID of notification */, notificationBuilder.build());
}

private void TextTTS(String message){
    String text2 = message;
    Log.d("Sdnalog", "TTS 말하는중?"+instanceTTS.isSpeaking());
    //http://stackoverflow.com/a/29777304
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        Log.d("Sdnalog", "TTS진입2");
        ttsGreater21(text2);
    } else {
        Log.d("Sdnalog", "TTS진입3");
        ttsUnder20(text2);
    }
    instanceTTS.shutdown();
}
private void ttsUnder20(String text) {
    HashMap<String, String> map = new HashMap<>();
    map.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "MessageId");
    Log.d("Sdnalog", "TTS speak");
    instanceTTS.speak(text, TextToSpeech.QUEUE_FLUSH, map);
}

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void ttsGreater21(String text) {
    String utteranceId=this.hashCode() + "";
    Log.d("Sdnalog", "TTS speak");
    instanceTTS.speak(text, TextToSpeech.QUEUE_FLUSH, null, utteranceId);
}
public class SdnaOnInitListener implements TextToSpeech.OnInitListener
{
    String msg;
    public SdnaOnInitListener(String s) {
        msg = s;
        Log.d("Sdnalog", "SdnaOnInitListener 생성자");
    }
    @Override
    public void onInit ( int status){
        if (status == TextToSpeech.SUCCESS)
        {
            instanceTTS.setLanguage(Locale.KOREAN);
            Log.d("Sdnalog", "초기화 완료");
            TextTTS(msg);
        }
    }
}

@Override
public void onDestroy() {
    Log.d("slog", "onDestroy()");
    super.onDestroy();
    //Toast toast = Toast.makeText(context, "서비스 onDestroy", Toast.LENGTH_SHORT);
    //toast.show();
}
@Override
public void onCreate() {
    super.onCreate();
    Log.d("slog", "onCreate()");
    context = getApplicationContext();
    //Toast toast = Toast.makeText(context, "서비스 onCreate", Toast.LENGTH_SHORT);
    // toast.show();
}
@Override
public void onStart(Intent intent, int startId) {
    Log.d("slog", "onStart()");
    super.onStart(intent, startId);
    //Toast toast = Toast.makeText(context, "서비스 onStart", Toast.LENGTH_SHORT);
    //toast.show();
}

public Intent SetActivity(String a){
    Intent intent;
    String text;
    if(a==null){text="menu";}
    else{text=a;}
    switch (text){
        case  "menu":
            intent =  new Intent(this, MenuActivity.class);
            break;
        case  "air":
            intent =  new Intent(this, AirSensorList.class);break;
        case  "body":
            intent =  new Intent(this, BodySensorList.class);break;
        case  "commu":
            intent =  new Intent(this, CommuSensorList.class);break;
        case  "engine":
            intent =  new Intent(this, EngineSensorList.class);break;
        case  "gas":
            intent =  new Intent(this, GasSensorList.class);break;
        case  "lower":
            intent =  new Intent(this, LowerSensorList.class);break;
        case  "steering":
            intent =  new Intent(this, SteeringSensorList.class);break;
        case  "trans":
            intent =  new Intent(this, TransmissionSensorList.class);break;
        case  "map":
            intent =  new Intent(this, MapActivity.class);break;
        case  "notilog":
            intent =  new Intent(this, NotiLogList.class);break;
        default:
             intent = new Intent(this,MenuActivity.class);break;
    }

    return  intent;
}


public void get_voice() {

    Thread thread = new Thread(new Runnable() {
        public void run() {
            handler.sendEmptyMessage(0);
        }
    });
    thread.start();
}

private Handler handler = new Handler() {
    public void handleMessage(Message msg) {

        listener = new RecognitionListener() {
            @Override
            public void onReadyForSpeech(Bundle bundle) {

            }

            @Override
            public void onBeginningOfSpeech() {

            }

            @Override
            public void onRmsChanged(float v) {

            }

            @Override
            public void onBufferReceived(byte[] bytes) {

            }

            @Override
            public void onEndOfSpeech() {

            }

            @Override
            public void onError(int i) {

            }

            @Override
            public void onResults(Bundle bundle) {
                String key = "";
                key = SpeechRecognizer.RESULTS_RECOGNITION;
                ArrayList<String> mResult = bundle.getStringArrayList(key);
                String[] rs = new String[mResult.size()];
                mResult.toArray(rs);
                //tv.setText(""+rs[0]);
                if(rs[0].contains("예")||rs[0].contains("응")||rs[0].contains("그래")){
                    Context context = getApplicationContext();

                    Intent intent = SetActivity(moveactivity);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startActivity(intent);
                }

            }

            @Override
            public void onPartialResults(Bundle bundle) {

            }

            @Override
            public void onEvent(int i, Bundle bundle) {

            }
        };

        i = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        i.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, getPackageName());
        i.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "ko-KR");

        mRecognizer  = SpeechRecognizer.createSpeechRecognizer(context);
        mRecognizer.setRecognitionListener(listener);
        mRecognizer.startListening(i);

        // sendNotification(NotiApplyItem item) << 메소드 호출
        msg = null;
    }
};
}
AL.
  • 36,815
  • 10
  • 142
  • 281
  • Have you determined what are the circumstances when the TTS function works and when it doesn't? – AL. Jan 17 '17 at 02:34
  • my intent are I't works anytime when FCM message recieved – Na sung Dae Jan 17 '17 at 02:42
  • Sometimes it works perfectly under the same conditions and sometimes it is not. – Na sung Dae Jan 17 '17 at 02:58
  • Hi. It's really hard to comment on anything. So far it's just "*This code works, sometimes it doesn't*". Do you at least have any suspicion as to what is causing the *unexpected* behavior? – AL. Jan 17 '17 at 03:00
  • The first step is to isolate the problem. When it doesn't work, can you see if the message has been received at all? If the message was received, but not spoken: it's a TTS problem (and FCM is not related). If the message wasn't received, it's an FCM problem (and not related to TTS). – Frank van Puffelen Jan 17 '17 at 03:01

0 Answers0