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

FCM 푸시 메시지 클릭시 이벤트 설정하는 방법.. [closed]

0 추천

안녕하세요,

FCM을 이용해서 등록된 앱들에게 메시지, 이미지 등 보내는 것은 해결했는데요.

만약 해당 이미지, 푸시 메시지를 눌렀을 때, 지정한 웹페이지를 열려고하는데요.

 

웹에서 푸시서버로 메시지를 보낼 때는,

...
$imgsrc ="이미지경로;
$link = "푸시 눌렀을 때 이동할 페이지주소";
$message = "푸시메시지";

$message = array("message" => $message, "imgsrc" => $imgsrc, "link" => $link);

$message_result = send_notification($tokens, $message);

...

이런식으로 message 배열을 통해서 링크 페이지 주소도 같이 보냅니다.

 

그리고 안드로이드에서 받는 onMessageReceived 함수에서

...

  imgsrc = remoteMessage.getData().get("imgsrc");
  link = remoteMessage.getData().get("link");
  sendNotification(remoteMessage.getData().get("message"));

...

 

문자열 변수 두 개 앞에 선언해서 푸시에서 받은 변수에 저장된 값들을 받아 저장시킨뒤에..

sendNotification 함수에서

      ....

        // 기존용 : Intent intent = new Intent(this, MainActivity.class);

        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(link));
        Log.d("Link", link);


        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
                PendingIntent.FLAG_ONE_SHOT);

        ....


            NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setStyle(style)
                    .setContentTitle("알림이 도착했습니다.")
                    .setContentText("밑으로 드래그해주세요.")
                    .setAutoCancel(true)
                    .setSound(defaultSoundUri)
                    .setDefaults(NotificationCompat.DEFAULT_VIBRATE)
                    .setContentIntent(pendingIntent);

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


            notificationManager.notify(0
                    // ID of notification
                    , notificationBuilder.build());

           ...

이런식으로 실행하면 메시지를 눌러도 페이지가 열리거나 하지는 않더라구요. startActivity(intent)를 하게 되면 어플이 꺼져버리구요..

 

그래서 기존에 있던 템플릿을 보니 PendingIntent를 사용해서 NotificationCompat.Builder에 setContentIntent(PendingIntent)로 설정해두던데.. 이 부분이 클릭했을 때 동작 부분을 설정할 수 있는 부분이 맞나요..?

해당 푸시메시지를 클릭했을 때 웹페이지를 새로 열려면 어떤 방식으로 가야하는지 궁금합니다 (__)

이런 경우에도 어플이 먼저 실행된 다음 웹페이지를 열어야하나요? 웹으로 바로 연결은 없는건지.. 둘 다 가능한지 궁금합니다 :D

질문을 종료한 이유: 답변 없음
겸군님 (1,900 포인트) 님이 2016년 10월 5일 질문
겸군님님이 2017년 2월 3일 closed
...