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

FCM push noti에서 팝업 알림창 안뜸 현상.

0 추천

안녕하세요

안드로이드 고수님들. 

현재 php -> FCM -> android단으로 정보를 처리하고 있으며,

FCM에서 android단으로 push해줄때 

O -OS : 알림음, noti 팝업창, noti 모두 정상적으로 뜸.

N -OS : [알림음, noti 팝업창] 안뜸 , noti만 정상적으로 옴.

왜 이런 현상이 나타날까요?

 

-php code-

function send_fcm($message, $id)  {
    $url = 'https://fcm.googleapis.com/fcm/send';

    $headers = array (
            'Authorization: key=' . GOOGLE_SERVER_KEY,
            'Content-Type : application/json'
    );

    $fields = array ( 'data' => array("message" => $message), 'notification' => array ("body" => $message));

    if(is_array($id)) {
        $fields['registration_ids'] = $id;
    } else {
        $fields['to'] = $id;
    }

    $fields['priority'] = "high";

    $fields = json_encode($fields);

    $ch = curl_init();
    curl_setopt ( $ch, CURLOPT_URL, $url );
    curl_setopt ( $ch, CURLOPT_POST, true );
    curl_setopt ( $ch, CURLOPT_HTTPHEADER, $headers );
    curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
    curl_setopt ( $ch, CURLOPT_POSTFIELDS, $fields );

    curl_exec($ch);

    curl_close($ch);
    return $result;

}

 

 

 

-android code -

private void sendNotification(String messageBody) {
    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);

    String channelId = getString(R.string.default_notification_channel_id);
    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder =
            new NotificationCompat.Builder(this, channelId)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setContentTitle("FCM MESSAGE")
                    .setContentText(messageBody)
                    .setAutoCancel(true)
                    .setSound(defaultSoundUri)
                    .setPriority(NotificationCompat.PRIORITY_HIGH)
                    .setContentIntent(pendingIntent);

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

    // Since android Oreo notification channel is needed.
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        // Create channel to show notifications.
        String channelName = getString(R.string.default_notification_channel_name);
        NotificationChannel channel = new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_HIGH);
        notificationManager.createNotificationChannel(channel);
    }

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

 

고수님들의 조언 부탁드립니다.

*추가로 설명을 드리면 현재 개발 중인 앱이 targetSDK는 26이며 FCM console로 메시지를 보내도 

 두 os에서 동일한 현상이 나타나고 있습니다..ㅜㅜ*

익명사용자 님이 2018년 8월 27일 질문
2018년 8월 27일 수정

1개의 답변

0 추천
  • 자체적인 해결 방법을 찾았습니다.

    android fcm 은

     

    앱 상태알림데이터모두
    포그라운드onMessageReceivedonMessageReceivedonMessageReceived
    백그라운드작업 표시줄onMessageReceived알림: 작업 표시줄
    데이터: 인텐트 부가 정보

     

    app이 background일떄는 알림을 작업 표시줄로 보여주도록 되어 있는게 맞더라구요.

    하지만 O OS부터 channel이라는 개념이 생성되고 나서 잘은 모르겠지만 한번 channel이 생성이 되면 popup이 background에 있어도 계속 생성이 되는것 같습니다..

     

    하지만 O이하(API 26)에서는 앱이 background나 kill되었을떄는 기본적인 정책에 의해 작업표시줄에 보여지는것이 맞습니다.

    저처럼 background나 kill이 되었을때도 noti popup이 보여주게 하고 싶다면

    data payload에 noti관련 정보를 전달해주면 안됩니다.

     

     

        $fields = array ( 'data' => array("message" => $message));
        if(is_array($id)) {
            $fields['registration_ids'] = $id; } else {
            $fields['to'] = $id; }

    //    $fields['priority'] = "high";

     

    php code를 위와 같으 수정해주시고,

     

     

    if (remoteMessage.getData().size() > 0) {
        Log.d(TAG, "Message data payload: " + remoteMessage.getData().get("message"));
        sendNotification(remoteMessage.getData().get("message"));
    
        if (/* Check if data needs to be processed by long running job */ true) {
            // For long-running tasks (10 seconds or more) use Firebase Job Dispatcher.
        } else {
            // Handle message within 10 seconds
            handleNow();
        }
    
    }
    
    // Check if message contains a notification payload.
    if (remoteMessage.getNotification() != null) {
        Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
    //    sendNotification(remoteMessage.getNotification().getBody());
    }

     

    android onMessageReceived에서  위와같이 처리해주시면 됩니다!

     

    이상 저와같은 착오를 하시는 분이 없으시길 바라며..


     

     

익명사용자 님이 2018년 8월 27일 답변
...