안드로이드) Firebase 푸시 메시지 종류에 따라 실행되는 액티비티를 변경하고 싶습니다.

조회수 1645회

안녕하세요, Firebase 푸시 메시지 기능을 사용하던 도중 궁금한 게 생겨 문의드립니다.

현재 커뮤니티 앱을 만들고 있는데,

  1. 게시판에 글을 올렸을 경우
  2. 1:1 채팅 메시지를 보냈을 경우
  3. 1:多 채팅 메시지를 보냈을 경우에 각각 다른 내용의 푸시 메시지를 전송하고 있습니다.

그런데, 각각의 푸시 메시지는 내용만 다를 뿐 모두 LoginActivity로 인텐트가 넘어가게 됩니다.

혹시,

  1. 게시판에 글을 올렸을 때 -> BoardFragment.class
  2. 1:1 채팅 메시지를 보냈을 경우 -> MessageActivity.class
  3. 1:多 채팅 메시지를 보냈을 경우 -> GroupMessageAcitivty.class로 각자 넘길 수 있을까요?

메소드 오버라이딩을 시도해보긴 했으나 오버라이딩 된 메소드를 실행할 수가 없네요... 실행 코드는 다음과 같습니다.

감사합니다.

private void sendNotification(String title, String text) {

    // 채널 생성 - skd 26? 부터
    // todo
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
        // 푸시메시지를 누를 경우 로그인 액티비티로 이동합니다.
        Intent intent = new Intent(this, LoginActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
                PendingIntent.FLAG_ONE_SHOT);

        Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder =
                new NotificationCompat.Builder(this)
                        .setSmallIcon(R.drawable.ic_share_black_24dp)
                        .setContentTitle(title)
                        .setContentText(text)
                        .setAutoCancel(true)
                        .setSound(defaultSoundUri)
                        .setVibrate(new long[]{100, 200, 100, 200})
                        .setPriority(Notification.PRIORITY_HIGH)
                        // 채널 적용
                        .setChannelId(ChannerId)
                        .setContentIntent(pendingIntent);

        // 채널 적용
        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        NotificationChannel notificationChannel
                = new NotificationChannel(ChannerId, "ChattingApp", NotificationManager.IMPORTANCE_DEFAULT);
        notificationChannel.setDescription("서비스 전략경영학회(SSMA)입니다.");
        notificationChannel.enableLights(true);
        notificationChannel.setLightColor(Color.GREEN);
        notificationChannel.enableVibration(true);
        notificationChannel.setVibrationPattern(new long[]{100, 200, 100, 200});
        notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);

        if (notificationManager != null) {
            notificationManager.createNotificationChannel(notificationChannel);
        }

        if (notificationManager != null) {
            notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
        }
    }

    // 채널 생성 - < 26
    else {
        // 푸시메시지를 누를 경우 로그인 액티비티로 넘어갑니다.
        // todo
        Intent intent = new Intent(this, LoginActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
                PendingIntent.FLAG_ONE_SHOT);

        Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder =
                new NotificationCompat.Builder(this)
                        .setSmallIcon(R.drawable.ic_share_black_24dp)
                        .setContentTitle(title)
                        .setContentText(text)
                        .setAutoCancel(true)
                        .setSound(defaultSoundUri)
                        .setVibrate(new long[]{100, 200, 100, 200})
                        .setPriority(Notification.PRIORITY_HIGH)
                        // 채널 적용
                        // .setChannelId(ChannerId)
                        .setContentIntent(pendingIntent);

        // 채널 적용
        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
    }
}
  • (•́ ✖ •̀)
    알 수 없는 사용자
  • LoginActivity 에 putExtra 로 실행 될 화면 분기를 위한 특정 값을 넣어서 Intent 를 실행하고, LoginActivity 에서 getIntent() 로 해당 값을 가져온 뒤 원하는 화면들로 분기해서 실행 시키면 될 것 같습니다. 알 수 없는 사용자 2019.1.1 20:35
  • 아, sendNotification 메소드 내에 각각의 기능에 맞게 putExtra로 특정 값을 넣은 뒤 LoginActivity에서 해당 값을 받을 수 있을 경우 바로 각각의 액티비티로 넘겨주면 된다는 말씀이신거죠? 감사합니다, 이해했습니다!!! 알 수 없는 사용자 2019.1.1 23:14

1 답변

  • 해결했습니다. 혹시 같은 어려움을 겪는 분들이 계시다면 도움이 될 것 같습니다.

    푸시 메시지를 보내는 메소드에서 값들을 넘겨주고, onMessageReceived() 코드를 수정하면 됩니다.

    public void onMessageReceived(RemoteMessage remoteMessage) {
        if (remoteMessage.getData().size() > 0) {
            String title = remoteMessage.getData().get("title");
            String text = remoteMessage.getData().get("text");
            String caseNumber = remoteMessage.getData().get("caseNumber");
            String index = remoteMessage.getData().get("index");
    
            // 1:1 채팅방으로 떨어뜨립니다.
            if (caseNumber.equals("0")) {
                sendNotification0(title, text, index);
            }
    
            // 1:多 채팅방으로 떨어뜨립니다.
            if (caseNumber.equals("1")) {
                sendNotification1(title, text, index);
            }
    
            // 게시판으로 떨어뜨립니다.
            if (caseNumber.equals("2")) {
                sendNotification2(title, text);
            }
        }
    }
    
    • (•́ ✖ •̀)
      알 수 없는 사용자

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

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

(ಠ_ಠ)
(ಠ‿ಠ)