마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

TTS가 디버그모드일때는 잘 동작하는데 그냥실행할때는 안됩니다

0 추천

푸시알림을 받으면 TTS로 알림의 내용을 읽어주는 앱을 만들고 있습니다

디버그 모드시에는 TTS로 원하는 String값이 들어가 TTS가 잘 작동하는데

반복적으로 같은 푸시를 받으며 테스트 하는중에 usb만빼고 실행해 보면 usb를 뺀뒤부터 TTS가 동작하지 않습니다.

이유가 뭘까요?

public class FirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService {
    TextToSpeech tts;
    private static final String TAG = "FirebaseMsgService";

    // [START receive_message]
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        noti(remoteMessage);
        
    }
    private void TextTTS(String message){
        tts=new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int status) {
                if(status != TextToSpeech.ERROR) {
                    tts.setLanguage(Locale.KOREAN);
                }
            }
        });
        String text = "알림이 도착했습니다";
        String text2 = message;

        //http://stackoverflow.com/a/29777304
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            ttsGreater21(text2);
        } else {
            ttsUnder20(text2);
        }
/*
        if(tts !=null){
            tts.stop();
            tts.shutdown();
        }*/
    }


   

    public void noti(RemoteMessage remoteMessage) {

        Map<String, String> bundle = remoteMessage.getData();
        System.out.println(bundle.toString());
        String title = bundle.get("title"); //노드 서버에서 message에서 title 받아오기
        String message = bundle.get("message");
        String location = bundle.get("location");
        //Double lat = Double.valueOf(bundle.get("lat"));//노드 서버에서 message에서 message 받아오기
        //Double lot = Double.valueOf(bundle.get("lot"));//노드 서버에서 message에서 message 받아오기


        Intent intent = new Intent(this, MainActivity.class);//푸시알람 클릭시 띄울 페이지
        //intent.putExtra("location",location);
        //intent.putExtra("lat", lat); // 인텐트에 데이터 위도 담아주기
        //intent.putExtra("lot", lot); // 인텐트에 데이터 경도 담아주기

        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); //MainActiviy 위쪽의 스택을 모두 제거
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
                PendingIntent.FLAG_ONE_SHOT);  //PendingIntent를 FLAG_ONE_SHOT(일회용)

        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(android.R.drawable.ic_dialog_email) //푸시 아이콘 설정
                .setContentTitle(title) //푸시 타이틀 이름
                .setContentText(message)//푸시 서브 타이틀
                .setAutoCancel(true)//선택시 자동제거?
                .setSound(defaultSoundUri) // 도착시 알람
                .setContentIntent(pendingIntent); // PendingIntent 정의
        TextTTS(message);


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

        notificationManager.notify(0 /* ID of notification */, notificationBuilder.build()); //푸시 빌더를 매니저로 넘겨주기 (푸시 실행)


        // TODO(developer): Handle FCM messages here.
        // Not getting messages here? See why this may be: https://goo.gl/39bRNJ
        Log.d(TAG, "From: " + remoteMessage.getFrom());

        // Check if message contains a data payload.
        if (remoteMessage.getData().size() > 0) {
            Log.d(TAG, "Message data payload: " + remoteMessage.getData());
        }

        // Check if message contains a notification payload.
        if (remoteMessage.getNotification() != null) {
            Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
        }

        // Also if you intend on generating your own notifications as a result of a received FCM
        // message, here is where that should be initiated. See sendNotification method below.
    }// [END noti]

    


    @SuppressWarnings("deprecation")
    private void ttsUnder20(String text) {
        HashMap<String, String> map = new HashMap<>();
        map.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "MessageId");
        tts.speak(text, TextToSpeech.QUEUE_FLUSH, map);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    private void ttsGreater21(String text) {
        String utteranceId=this.hashCode() + "";
        tts.speak(text, TextToSpeech.QUEUE_FLUSH, null, utteranceId);
    }

}

 

 

신입 (570 포인트) 님이 2016년 11월 22일 질문
신입님이 2016년 11월 22일 수정

답변 달기

· 글에 소스 코드 보기 좋게 넣는 법
· 질문에 대해 추가적인 질문이나 의견이 있으면 답변이 아니라 댓글로 달아주시기 바랍니다.
표시할 이름 (옵션):
개인정보: 당신의 이메일은 이 알림을 보내는데만 사용됩니다.
스팸 차단 검사:
스팸 검사를 다시 받지 않으려면 로그인하거나 혹은 가입 하세요.
...