안드로이드) FCM 푸시 메시지를 백그라운드에서 실행 시 onMessageReceived() 메소드가 실행되지 않습니다.

조회수 1528회

안녕하세요,

Firebase를 기반으로 푸시 메시지를 보낼 경우 앱이 포그라운드 상태에서는 FirebaseMessagingService의 onMessageReceived() 메소드가 실행되는데 백그라운드 상태에서는 실행되지 않아 원하는 intent값을 넘기지 못하고 있습니다.

같은 질문이 stackoverflow 같은 페이지에서도 많아 해답을 확인할 수 있었으나 따라할 수가 없어 문의드립니다!

firebase API에 대한 요청의 JSON 키에 'notification'을 넣지 말고 아래와 같이 'data'를 사용하면 해결된다고 알고는 있는데 해당 코드들을 어디서 확인해야 하는지 도저히 모르겠습니다...

{ "to" : "/topics/topic_name", "data": { "key1" : "value1", "key2" : "value2", } }

또, 오레오 버전 이상의 기기들에서는 백그라운드 상태에서도 푸시메시지를 통해 정상적으로 원하는 기능들이 수행되는데 그 이유도 잘 모르겠습니다.........

제 코드 중 푸시 메시지를 보내는 코드는 다음과 같습니다.

void sendGcm() {
    Gson gson = new Gson();

    NotificationModel notificationModel = new NotificationModel();

    // 현재 유저 네임으로 메시지를 띄울 때 사용하기 위함입니다.
    String userName = FirebaseAuth.getInstance().getCurrentUser().getDisplayName();

    notificationModel.to = destinationUserModel.pushToken;
    notificationModel.notification.title = userName;
    notificationModel.notification.text = input_text.getText().toString();

    // 푸시 메시지 출력에 필요한 변수들입니다.
    notificationModel.data.title = userName;
    notificationModel.data.text = input_text.getText().toString();
    notificationModel.data.caseNumber = "0";
    notificationModel.data.index = uid;

    RequestBody requestBody =
            RequestBody.create(MediaType.parse("application/json; charset=utf8"),
                    gson.toJson(notificationModel));

    Request request = new Request.Builder()
            .header("Content-Type", "application/json")
            // 해당 서버키를 입력합니다.
            .addHeader("Authorization", "key=...")
            // 이것도 맞는지 확인해야 합니다.
            .url("https://gcm-http.googleapis.com/gcm/send")
            .post(requestBody)
            .build();

    OkHttpClient okHttpClient = new OkHttpClient();
    okHttpClient.newCall(request).enqueue(new Callback() {
        @Override
        public void onFailure(Call call, IOException e) {

        }

        @Override
        public void onResponse(Call call, Response response) throws IOException {

        }
    });
}

푸시 메시지를 보내는 데이터 모델 클래스는 다음과 같습니다.

public class NotificationModel {

public String to;

public Data data = new Data();
public Notification notification = new Notification();

public static class Notification {
    public String title;
    public String text;
}

public static class Data {
    public String title;
    public String text;
    public String caseNumber;
    public String index;
}

}

  • (•́ ✖ •̀)
    알 수 없는 사용자
  • 해결했습니다~ 알 수 없는 사용자 2019.1.6 14:52
  • 축하드립니다. 어떻게 해결했는지 공유해 주시면 어떨까요 엽토군 2019.1.6 22:28

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)