FCM을 이용해서 푸시알림을 날리면
알림이 뜰때 TTS 로 읽어주는 앱을 만들고 있습니다.
그런데 문제가 있습니다.
1. 앱이 화면에 띄워져 있을때만 TTS가 동작한다. ( String에 직접 '알림이 도착했습니다' 넣어서 확인)
2. 디버그모드시에는 TTS가 작동하지만 usb를 빼고 실행하면 1번의 TTS도 작동하지 않는다
4.앱이 백그라운드, 화면꺼짐상태로 가면 상단바에 푸시알림이 나오긴 하지만 TTS동작을 하지않으며 알람 소리도 나오지않는다.
5.푸시알림이 도착하면 화면이 켜지는 코드를 넣었지만 화면이 켜지지 않는다.
제가 원하는것은 앱이 백그라운드, 킬, 화면에 띄워짐 상태에 상관없이 상단바에 푸시알림이 나오고
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) {
sendPushNotification(remoteMessage.getData().get("message"));
}
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(text);
} else {
ttsUnder20(text);
}
}
private void sendPushNotification(String message) {
System.out.println("received message : " + message);
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
//.setSmallIcon(R.drawable.noti).setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher) )
.setContentTitle("Push Title ")
.setContentText(message)
.setAutoCancel(true)
.setSound(defaultSoundUri).setLights(000000255,500,2000)
.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(0 /* ID of notification */, notificationBuilder.build());
TextTTS(message);
}
@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);
}
}