FCM을 이용한 알림을 받는 앱을 만들었는데
원인불명의 현상이 나타납니다
1. 앱이 비활성화 시에는 Firebase대시보드에서 보낸 알림을 받을수 없습니다.
postman등으로 api를 직접 호출해 보낸 알림은 받을 수 있습니다.
2. 알림을 두번이상 받을 수 없습니다.
대시보드에서 보낸알림이건 API호출로 보낸 알림이건 앱을 한번 껏다키지 않는이상
알림을 받을 수 없습니다.
디버그모드로 확인해보니 onMessageReceived 로 진입하지도 않습니다(포그라운드 백그라운드 모두)
무슨원인인지 짐작도 안가고 물어볼곳도 없으니 답답한 노릇입니다.
정확하지않아도 짐작이라도 가는 작은 힌트라도 알려주세요
package com.example.sdna.charly;
/**
* Created by sdna on 2016-12-12.
*/
import android.annotation.TargetApi;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Build;
import android.speech.tts.TextToSpeech;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import com.google.firebase.messaging.RemoteMessage;
import java.util.HashMap;
import java.util.Locale;
public class MyFirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService {
private static final String TAG = "FirebaseMsgService";
TextToSpeech instanceTTS =null;
Context context;
// [START receive_message]
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
//instanceTTS =new TextToSpeech(getApplicationContext(), new SdnaOnInitListener(remoteMessage.getData().get("message")));
//추가한것
sendNotification(remoteMessage);
}
private void sendNotification(RemoteMessage remoteMessage) {
String message = remoteMessage.getData().get("message");
String title = remoteMessage.getData().get("title");
Intent intent = new Intent(this, MenuActivity.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.mipmap.ic_launcher)
.setContentTitle(title)
.setContentText(message)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* 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();
}
}