https://medium.com/nybles/sending-push-notifications-by-using-firebase-cloud-messaging-249aa34f4f4c

 

Sending Push Notifications by Using Firebase Cloud Messaging

Ever wondered how does your smartphone receive and handle notifications whether be in foreground, background or even killed?

medium.com

앱을 만들꺼다... 저번에 하던 geofences 관련된 앱도 만들고 있는 중이고, 진척이 더디다... 그래도 만들꺼다. 이번에는 FCM 을 이용해서   message  push 을 해 보고자 한다. 

다수의 사용자에게 또는 특정 사용자에게 메시지 전송을 하고 싶다.   FCM 의 문서를 읽어보면 서버 구성을 할 수 있으면 좋을 것 같다. 그러나 일반적인 사용자에게는 서버가 있을턱은 없고. 그럼 그냥 앱에서 그것들을 구현해 보는 것이다. 

그럼 어떻게 할 것 인가 ?

https://stackoverflow.com/questions/37576705/firebase-java-server-to-send-push-notification-to-all-devices 

 

Firebase Java Server to send push notification to all devices

I am trying to send a Push notification to my android device with the new Firebase service. I registered and setup an app, also I put all the code needed to receive notification in the android app....

stackoverflow.com

어떤 주제를 가지고 공통의견을 가지는 사람들에게 알림을 보내는 방법.  일단 그 방법을 적용해 보기로 했다.

클라이언트 앱에서는 topic 을 2개 수신 등록을 해 두고 서버앱에서는 topic 을 이용해서 클라이언트 앱으로 메시지를 push 해 보는 것이다.

다수의 시간을 소비해 가면서 찾은 것들을 정리해 둠... 이런 저런 검색들 끝에 찾은건... 문서을 잘 읽어 보자. 그리고 서버가 없는 나는 어떻게 할 것 인가 ? 그것에 대한 고민을 해 보자. ㅠㅠ

아래 소스는 오늘 저녁 4시간 가량을 소비해 가면서 구글링 한 결과 얻은 소스을 일부 수정했다. android apps 에서 사용할 수 있도록 

저 class 을 따로 하나 만들어서 내가 사용하고 싶은 곳에서 호출해서 사용하면 된다.

import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;

import com.google.auth.oauth2.GoogleCredentials;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Arrays;
import java.util.Scanner;

/**
 * Firebase Cloud Messaging (FCM) can be used to send messages to clients on iOS, Android and Web.
 *
 * This sample uses FCM to send two types of messages to clients that are subscribed to the `news`
 * topic. One type of message is a simple notification message (display message). The other is
 * a notification message (display notification) with platform specific customizations, for example,
 * a badge is added to messages that are sent to iOS devices.
 */
public class Messaging {

    private static final String PROJECT_ID = "my-appl0fb";
    private static final String BASE_URL = "https://fcm.googleapis.com";
    private static final String FCM_SEND_ENDPOINT = "/v1/projects/" + PROJECT_ID + "/messages:send";

    private static final String MESSAGING_SCOPE = "https://www.googleapis.com/auth/firebase.messaging";
    private static final String[] SCOPES = { MESSAGING_SCOPE };

    private static final String TITLE = "FCM Notification";
    private static final String BODY = "Notification from FCM";
    public static final String MESSAGE_KEY = "message";

    public Messaging() {
        
    }

    /**
     * Retrieve a valid access token that can be use to authorize requests to the FCM REST
     * API.
     *
     * @return Access token.
     * @throws IOException
     */
    // [START retrieve_access_token]
    public String getAccessToken(Context context) throws IOException {

        AssetManager assetManager = context.getAssets();
        AssetFileDescriptor fileDescriptor = assetManager.openFd("my-ap0fb.json");
        FileInputStream fileInputStream = fileDescriptor.createInputStream();

        GoogleCredentials googleCredentials = GoogleCredentials
                .fromStream(fileInputStream)
                .createScoped(Arrays.asList(SCOPES));
        // googleCredentials.refreshAccessToken().getTokenValue();
        return googleCredentials.refreshAccessToken().getTokenValue();
    }
    // [END retrieve_access_token]

    /**
     * Create HttpURLConnection that can be used for both retrieving and publishing.
     *
     * @return Base HttpURLConnection.
     * @throws IOException
     */
    public HttpURLConnection getConnection(Context context) throws IOException {
        // [START use_access_token]
        URL url = new URL(BASE_URL + FCM_SEND_ENDPOINT);
        HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
        httpURLConnection.setRequestProperty("Authorization", "Bearer " + getAccessToken(context));
        httpURLConnection.setRequestProperty("Content-Type", "application/json; UTF-8");
        return httpURLConnection;
        // [END use_access_token]
    }

    /**
     * Send request to FCM message using HTTP.
     * Encoded with UTF-8 and support special characters.
     *
     * @param fcmMessage Body of the HTTP request.
     * @throws IOException
     */
    public void sendMessage(JsonObject fcmMessage, Context context) throws IOException {
        HttpURLConnection connection = getConnection(context);
        connection.setDoOutput(true);
        OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");
        writer.write(fcmMessage.toString());
        writer.flush();
        writer.close();

        int responseCode = connection.getResponseCode();
        if (responseCode == 200) {
            String response = inputstreamToString(connection.getInputStream());
            System.out.println("Message sent to Firebase for delivery, response:");
            System.out.println(response);
        } else {
            System.out.println("Unable to send message to Firebase:");
            String response = inputstreamToString(connection.getErrorStream());
            System.out.println(response);
        }
    }

    /**
     * Send a message that uses the common FCM fields to send a notification message to all
     * platforms. Also platform specific overrides are used to customize how the message is
     * received on Android and iOS.
     *
     * @throws IOException
     */
    public void sendOverrideMessage(Context context) throws IOException {
        JsonObject overrideMessage = buildOverrideMessage();
        System.out.println("FCM request body for override message:");
        prettyPrint(overrideMessage);
        sendMessage(overrideMessage, context);
    }

    /**
     * Build the body of an FCM request. This body defines the common notification object
     * as well as platform specific customizations using the android and apns objects.
     *
     * @return JSON representation of the FCM request body.
     */
    public JsonObject buildOverrideMessage() {
        JsonObject jNotificationMessage = buildNotificationMessage();

        JsonObject messagePayload = jNotificationMessage.get(MESSAGE_KEY).getAsJsonObject();
        messagePayload.add("android", buildAndroidOverridePayload());

        JsonObject apnsPayload = new JsonObject();
        apnsPayload.add("headers", buildApnsHeadersOverridePayload());
        apnsPayload.add("payload", buildApsOverridePayload());

        messagePayload.add("apns", apnsPayload);

        jNotificationMessage.add(MESSAGE_KEY, messagePayload);

        return jNotificationMessage;
    }

    /**
     * Build the android payload that will customize how a message is received on Android.
     *
     * @return android payload of an FCM request.
     */
    public JsonObject buildAndroidOverridePayload() {
        JsonObject androidNotification = new JsonObject();
        androidNotification.addProperty("click_action", "android.intent.action.MAIN");

        JsonObject androidNotificationPayload = new JsonObject();
        androidNotificationPayload.add("notification", androidNotification);

        return androidNotificationPayload;
    }

    /**
     * Build the apns payload that will customize how a message is received on iOS.
     *
     * @return apns payload of an FCM request.
     */
    public JsonObject buildApnsHeadersOverridePayload() {
        JsonObject apnsHeaders = new JsonObject();
        apnsHeaders.addProperty("apns-priority", "10");

        return apnsHeaders;
    }

    /**
     * Build aps payload that will add a badge field to the message being sent to
     * iOS devices.
     *
     * @return JSON object with aps payload defined.
     */
    public JsonObject buildApsOverridePayload() {
        JsonObject badgePayload = new JsonObject();
        badgePayload.addProperty("badge", 1);

        JsonObject apsPayload = new JsonObject();
        apsPayload.add("aps", badgePayload);

        return apsPayload;
    }

    /**
     * Send notification message to FCM for delivery to registered devices.
     *
     * @throws IOException
     */
    public void sendCommonMessage(Context context) throws IOException {
        JsonObject notificationMessage = buildNotificationMessage();
        System.out.println("FCM request body for message using common notification object:");
        prettyPrint(notificationMessage);
        sendMessage(notificationMessage, context);
    }

    /**
     * Construct the body of a notification message request.
     *
     * @return JSON of notification message.
     */
    public JsonObject buildNotificationMessage() {
        JsonObject jNotification = new JsonObject();
        jNotification.addProperty("title", TITLE);
        jNotification.addProperty("body", BODY);

        JsonObject jMessage = new JsonObject();
        jMessage.add("notification", jNotification);
        jMessage.addProperty("topic", "allDevices");

        JsonObject jFcm = new JsonObject();
        jFcm.add(MESSAGE_KEY, jMessage);

        return jFcm;
    }

    /**
     * Read contents of InputStream into String.
     *
     * @param inputStream InputStream to read.
     * @return String containing contents of InputStream.
     * @throws IOException
     */
    public String inputstreamToString(InputStream inputStream) throws IOException {
        StringBuilder stringBuilder = new StringBuilder();
        Scanner scanner = new Scanner(inputStream);
        while (scanner.hasNext()) {
            stringBuilder.append(scanner.nextLine());
        }
        return stringBuilder.toString();
    }

    /**
     * Pretty print a JsonObject.
     *
     * @param jsonObject JsonObject to pretty print.
     */
    public void prettyPrint(JsonObject jsonObject) {
        Gson gson = new GsonBuilder().setPrettyPrinting().create();
        System.out.println(gson.toJson(jsonObject) + "\n");
    }

    /*
    public static void main(String[] args) throws IOException {
        if (args.length == 1 && args[0].equals("common-message")) {
            sendCommonMessage();
        } else if (args.length == 1 && args[0].equals("override-message")) {
            sendOverrideMessage();
        } else {
            System.err.println("Invalid command. Please use one of the following commands:");
            // To send a simple notification message that is sent to all platforms using the common
            // fields.
            System.err.println("./gradlew run -Pmessage=common-message");
            // To send a simple notification message to all platforms using the common fields as well as
            // applying platform specific overrides.
            System.err.println("./gradlew run -Pmessage=override-message");
        }
    }
    */

}

소스의 내용중에 main 함수는 막았다. 왜냐면 android 내에서는 호출할 일이 없으니까. 다만 원래 있던 부분이니 혹시나 서버용으로 구성을 하는 경우 필요할 듯 하여...

이제 앱 수준의 gradle 파일에 추가를 해야 한다.

plugins {
    id 'com.android.application'
    id 'com.google.gms.google-services'
}

android {
    compileSdk 30

    buildFeatures {
        viewBinding true
    }

    packagingOptions {
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/license.txt'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/INDEX.LIST'
        exclude 'META-INF/notice.txt'
        exclude 'META-INF/ASL2.0'
    }

    aaptOptions {
        noCompress "json"
    }

}

dependencies {

    implementation 'com.google.auth:google-auth-library-oauth2-http:1.0.0'

    implementation platform('com.google.firebase:firebase-bom:28.2.1')
    implementation 'com.google.firebase:firebase-messaging'
    implementation 'com.google.firebase:firebase-analytics'

}

원래는 다른 것들도 많이 있지만, 이것들은 꼭 필요한 듯 하여...

aaptOptions 는 사용법이 파일 확장자를 등록하는 것인데, assets 폴더에 json 파일을 넣어주고 그걸 읽어오게 하려고 했더니 file not found 오류가 나서 찾다가 압축을 하지 않도록 설정하는 것이란다. 그래야 제대로 파일을 읽어올 수 있다고

그 assets 폴더에 들어가야 하는 파일은 인증을 시도하기 위해서 저장해야 하는 파일인데, 이건 messaging class 에 보면 getAccessToken 함수에서 사용하고 있으니 거길 보시면 되고 거기에 보면 openFd() 에 파일이름이 있는데, 원래 이름은 내려받은 파일 이름으로 작성하시면 됨 이파일은 

firebase console 에서 해당 프로젝트 설정 의 서비스 계정에 보면 새 비공개 키생성 이라고 있는데, 요기를 클릭하면 내려주는 json 파일인데, 이걸 받아서 이름을 변경하여 저장한 것을 내 프로젝트의 폴더에 담았다.

위치는 아래 그림 처럼 내 프로젝트 app/src/main/assets 폴더에 다가...

 

 

 

 

이제 준비는 된 것 같으니... MainActivity 에 호출하는 함수를 구현해 본다.

        new Thread() {
            @Override
            public void run() {
                super.run();
                try {

                    Messaging messaging = new Messaging(getApplicationContext());
                    messaging.sendCommonMessage(getApplicationContext());

                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        }.start();

thread 을 사용하는 건 아시겠지만, android 가 http 통신을 하기 위해서는 비동기식으로 처리를 해야만 오류가 발생하지 않기 때문에 이렇게 처리를 하는 것이다.

혹시나 여기 까지를 했는데, 앱에서 수신을 하지 않는다면... Messaging 소스에서 topic 이라는 단어를 찾어 보시길, 이걸 설정하는 이유는 메시지 수신할 경우를 지정하기 위해서 인데, 앱에서 특정한 방법으로 메시지를 전송하려면 구현을 이런 방식으로 해 두는 것이 좋을 것 같기도 하고.  암튼 저 메시지가 수신되는 것으로 확인하고 싶으면 topic 을 먼저 구독 독 하도록 설정해야 하는데, 그건

        FirebaseMessaging.getInstance().subscribeToTopic("allDevices")
                .addOnCompleteListener(new OnCompleteListener<Void>() {
                    @Override
                    public void onComplete(@NonNull Task<Void> task) {
                        Log.e(TAG, "allDevices subscribed ..." );
                    }
                });

MainActivity 의 onCreate 어딘가 쯤 위 코드를 넣어주면 앱이 시작 되면서 구독할 준비가 된다.

앱에서 메시지 수신에 대한 설명은 문서를 참조하여 작성하시길.  그건 아래 링크를 참조하시도록 남겨 놓는다.

https://firebase.google.com/docs/cloud-messaging/android/receive?authuser=0 

 

Android 앱에서 메시지 수신  |  Firebase

Firebase 알림의 동작은 수신하는 앱의 포그라운드/백그라운드 상태에 따라 달라집니다. 포그라운드 상태인 앱에서 알림 메시지 또는 데이터 메시지를 수신하려면 onMessageReceived 콜백을 처리하는

firebase.google.com

 

FCM 의 수신을 하는 경우 봐 두어야 하는 부분이 앱이 backgound 상태인 경우와 foreground 상태 메시지가 도달 했을 때 처리 되는 기준이 좀 다르다는 것을 알게 되었다. 위 링크에서 일부 설명은 되어 있지만 친절하게 설명이 되어 있지 않기 때문에 조금 준비를 하는 데 시간이 좀 필요하게 되었다. 

먼저 foreground 에서 수신 하는 경우에는  위 문서에서도 설명하고 있는 FirebaseMessagingService 에 onMessageReceived 을 통해 수신 되는 메시지를 이용해서 바로 보여 줄 수 있도록 구현을 하면 되었고,

@Override
    public void onMessageReceived(RemoteMessage remoteMessage) {

        // TODO(developer): Handle FCM messages here.
        // Not getting messages here? See why this may be: https://goo.gl/39bRNJ
        Log.e(TAG, "From: " + remoteMessage.getFrom());

        // Check if message contains a data payload.
        if (remoteMessage.getData().size() > 0) {
            Log.e(TAG, "Message data payload: " + remoteMessage.getData());

            sp = getSharedPreferences(getPackageName(), MODE_PRIVATE);
            editor = sp.edit() ;

            Map<String, String> strMap = remoteMessage.getData();
            Log.e(TAG, "URL=" + strMap.get("URL"));
            Log.e(TAG, "BODY=" + strMap.get("BODY"));

            editor.putString("URL", strMap.get("URL").contains("http") ? strMap.get("URL") : "https://" + strMap.get("URL"));
            editor.putString("BODY", strMap.get("BODY"));
            editor.putBoolean("FcmTy", true) ;
            editor.commit();

            handleNow();

        }

        // Check if message contains a notification payload.
        if (remoteMessage.getNotification() != null) {
            Log.e(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
            sendNotification(remoteMessage.getNotification().getBody()) ;
        }

        onDeletedMessages();
    }
    
    private void handleNow() {

        Log.d(TAG, "Short lived task is done.");
        Intent intent = new Intent(FcmReceiveService.this, ViewActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);

    }

 

background 상태일 때는 앱의 시스템 알림으로 notify 을 하게 되는데, FirebaseMessagingService 에서는 아래 코드 처럼 notification 을 구현하게 되어 시스템 알림이 뜨게 된다 이  떄 알림을 클릭하면 

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);

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

        String channelId = getString(R.string.default_notification_channel_id);
        CharSequence channelName = getString(R.string.default_notification_channel_name);
        int importance = NotificationManager.IMPORTANCE_LOW;
        NotificationChannel notificationChannel = new NotificationChannel(channelId, channelName, importance);
        notificationChannel.enableLights(true);
        notificationChannel.setLightColor(Color.BLUE);
        notificationChannel.enableVibration(true);
        notificationChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
        notificationManager.createNotificationChannel(notificationChannel);

        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder =
                new NotificationCompat.Builder(this, channelId)
                        .setSmallIcon(R.drawable.ic_launcher_foreground)
                        .setContentTitle(getString(R.string.fcm_message))
                        .setContentText(messageBody)
                        .setAutoCancel(true)
                        .setSound(defaultSoundUri)
                        .setContentIntent(pendingIntent);

        // Since android Oreo notification channel is needed.
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(channelId,
                    "Channel human readable title",
                    NotificationManager.IMPORTANCE_DEFAULT);
            notificationManager.createNotificationChannel(channel);
        }

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

 

문서에 기술된 것 처럼 LAUNCHER activity 를 호출 하도록 되어 있다.

백그라운드 앱에서 알림 메시지 처리
앱이 백그라운드 상태이면 Android에서 알림 메시지를 작업 표시줄로 전송합니다. 사용자가 알림을 탭하면 기본적으로 앱 런처가 열립니다.

여기에는 알림과 데이터 페이로드가 모두 들어 있는 메시지 및 알림 콘솔에서 보낸 모든 메시지가 포함됩니다. 이러한 경우 알림은 기기의 작업 표시줄로 전송되고 데이터 페이로드는 런처 활동의 인텐트 부가 정보로 전송됩니다.

앱으로 전송된 메시지의 통계를 파악하려면, iOS 및 Android 기기에서 열린 전송 메시지 수와 Android 앱의 '노출수'(사용자에게 표시된 알림) 데이터가 기록된 FCM 보고 대시보드를 확인합니다.

 

그래서 Launcher 로 등록된 MainAcitivity 가 실행 될 때 받아온 수신 문구를 처리하도록 구성해 주어야 한다. 

    @Override
    protected void onStart() {
        super.onStart();

        Log.e(TAG, "onStart") ;

        if (getIntent().getExtras() != null && !sp.getBoolean("FcmTy", false)) {
            Bundle bundle = getIntent().getExtras();
            Log.e(TAG, "URL=" + bundle.getString("URL"));
            Log.e(TAG, "BODY=" + bundle.getString("BODY"));
            editor.putString("URL", bundle.getString("URL").contains("http") ? bundle.getString("URL") : "https://" + bundle.getString("URL"));
            editor.putString("BODY", bundle.getString("BODY"));
            editor.putBoolean("FcmTy", true) ;
            editor.commit();
            Intent intent1 = new Intent(this, ViewActivity.class);
            startActivity(intent1);
            finish();
        }
    }

 

onCreate 에 하지 않고 onStart 에서 처리 하는 것은 아무래도 activity 의 lifecycle 에 따른 조치라고 볼 수 있을 것 같다.

 

그럼... 오늘도 즐~

반응형

 

앱을 하나 만들고 있다. geoFences 을 이용해서 내가 지금 어디에 있는가를 보고 자동으로 설정을 하거나 알림을 하는 앱을 ... 그런데, 난관(?)이 하나 생겼다.  집을 나오면 wifi을 끄고, 다시 집에 오면 wifi 을 자동으로 켜는 기능을 넣고 싶었는데,  내가 지금 쓰는 폰이 API가 29 (Android 10) 이상이라는 상황 때문에 wifimanager 의 기능중에 setWifiEnabled 을 사용할 수 없다는 것이다. 

흑~  다른 대안은 뭐가 있는가 ?  아직은 찾일을 수 없었다.

혹시나 찾게 되면 다음에 다시 적어 보도록 하겠다.

 

반응형
 View rootView = LayoutInflater.from(context).inflate(R.layout.row_timeline, null, false);
        RecyclerView.LayoutParams lp = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        rootView.setLayoutParams(lp);
        return new TimeLineViewHolder(rootView, viewType);

앱을 구현하다 보면 리스트를 목록 형태로 보여 주어야 하는 경우가 왕왕 발생 하게 된다.  이런 경우 주로 사용하는것이 예전에는 listview 을 많이 사용했는데, 이제는 recycleview 을 권장하는 시기가 되어 가는 것 같다.   나만의 생각일까 ?

뭐 암튼 recycleview 을 이용한 앱을 구현하는 동안 혼돈이 되었던 부분에 대한 기술을 해 두고자 한다.  먼저 recycleview 을 화면에 넣고 실행 했더니만...  layout 의 match_parent 가 적용 되지 않는다.  왜 ???

https://stackoverflow.com/questions/30691150/match-parent-width-does-not-work-in-recyclerview

 

match_parent width does not work in RecyclerView

My RecyclerView and item has match_parent width but the result is :

stackoverflow.com

잘 알 수는 없는 노릇이기는 하나,  아래 코드를 유의해서 보시길...

 View rootView = LayoutInflater.from(context).inflate(R.layout.row_timeline, null, false);
        RecyclerView.LayoutParams lp = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        rootView.setLayoutParams(lp);
        return new TimeLineViewHolder(rootView, viewType);

recycleview 에 setLayoutParams 을 이용해서 파라미터를 전달하는 방식으로 witdh 을 match_parent 을 적용하였다.  그러면 끝.   

다음은, adapter 까지 잘 구현했나 싶은데...  화면에 아무것도 나오지 않는다.

https://recipes4dev.tistory.com/154

 

안드로이드 리사이클러뷰 기본 사용법. (Android RecyclerView)

1. 안드로이드 리사이클러뷰(RecyclerView) 리사이클러뷰(RecyclerView)는, "많은 수의 데이터 집합을, 제한된 영역 내에서 유연하게(flexible) 표시할 수 있도록 만들어주는 위젯"입니다. [안드로이드 개발

recipes4dev.tistory.com

 

listview 을 사용할 때는 고민하지 않았던 문제인데,  

        RecyclerView recyclerView = findViewById(R.id.recycler1) ;
        recyclerView.setLayoutManager(new LinearLayoutManager(this)) ;

layoutManager 는 왜 지정해야 하는 가? 하는 의문이 들기는 하지만, layoutManager 설정이 되어 있지 않으면,  화면에 표시가 되지 않는다.  java 에서 지정할 때는 위의 코드처럼 구현 하면 되고, layout 에서 지정할 수도 있으니 참고하시길.

listview 에서는 이름처럼 list 모양으로만 view 을 구현할 수 있지만, recycleview 의 경우는 layoutmanager 을 이용해서 다양한 모양의 view 을 구성해 줄 수 있기 때문에 layoutmanager 을 설정해 주어야 했다. 

 

https://developer.android.com/guide/topics/ui/layout/recyclerview-custom

 

RecyclerView 맞춤설정  |  Android 개발자  |  Android Developers

RecyclerView의 고급 맞춤설정 옵션

developer.android.com

 

또한 아래 예제에서 보여 주고 있는 것 처럼 adapter 을 구현하는 소스의 모양도 조금 다르니  구조를 보고 이해를 해 두는 것이 좋을 것 같다.  listview 을 활용하는 경우의 adapter 구현을 모양과 비교를 해 보면 거의 비슷해 보이기는 하나,  ViewHolder 을 기본으로 구현해 주고 있으니 잘 활용할 수 있으면 좋을 것 같다.  ViewHolder 을 구현하는 이유는 자원 재활용에 도움이 된다고 했던 것으로 본 기억이 있다.  list 가 스크롤 되면서 화면 밖으로 넘어가면 없어지고 그 자리를 다른 데이터로 채우는 모양으로 자원을 재활용 한다는 설명을 어딘가에서 보았던 기억이 난다.

public class SimpleTextAdapter extends RecyclerView.Adapter<SimpleTextAdapter.ViewHolder> {

    private ArrayList<String> mData = null ;

    // 아이템 뷰를 저장하는 뷰홀더 클래스.
    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView textView1 ;

        ViewHolder(View itemView) {
            super(itemView) ;

            // 뷰 객체에 대한 참조. (hold strong reference)
            textView1 = itemView.findViewById(R.id.text1) ;
        }
    }

    // 생성자에서 데이터 리스트 객체를 전달받음.
    public SimpleTextAdapter(ArrayList<String> list) {
        mData = list ;
    }

    // onCreateViewHolder() - 아이템 뷰를 위한 뷰홀더 객체 생성하여 리턴.
    @Override
    public SimpleTextAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        Context context = parent.getContext() ;
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) ;

        View view = inflater.inflate(R.layout.recyclerview_item, parent, false) ;
        SimpleTextAdapter.ViewHolder vh = new SimpleTextAdapter.ViewHolder(view) ;

        return vh ;
    }

    // onBindViewHolder() - position에 해당하는 데이터를 뷰홀더의 아이템뷰에 표시.
    @Override
    public void onBindViewHolder(SimpleTextAdapter.ViewHolder holder, int position) {
        String text = mData.get(position) ;
        holder.textView1.setText(text) ;
    }

    // getItemCount() - 전체 데이터 갯수 리턴.
    @Override
    public int getItemCount() {
        return mData.size() ;
    }
}

 

이번에는 listview 을 지원하는 adapter 의 모양은 아래와 같다.

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import org.techtown.tab.databinding.ItemChattingListBinding;
import org.techtown.tab.model.ChatRooms;

import java.util.ArrayList;

public class ChatRoomAdapter extends BaseAdapter {

    ItemChattingListBinding binding ;
    ArrayList<ChatRooms> chattingListArrayList ;
    Context context ;
    LayoutInflater inflater;
    int nListCnt ;

    public ChatRoomAdapter(ArrayList<ChatRooms> oData, Context context) {
        this.chattingListArrayList = oData ;
        this.context = context ;
        this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return chattingListArrayList.size();
    }

    @Override
    public ChatRooms getItem(int position) {
        return chattingListArrayList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public void updateReceiptsList(ArrayList<ChatRooms> _oData) {
        chattingListArrayList = _oData;
        nListCnt = chattingListArrayList.size(); // 배열 사이즈 다시 확인
        this.notifyDataSetChanged(); // 그냥 여기서 하자
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        binding = ItemChattingListBinding.inflate(inflater);
        CustomViewHolder holder ;
        if (convertView == null) {
            convertView = binding.getRoot();
            holder = new CustomViewHolder();
            holder.board = binding.boardingLocation2 ;
            holder.dest = binding.destination2;
            holder.chatMember = binding.boardingPeopleNum2;
            convertView.setTag(holder);
        } else {
            holder = (CustomViewHolder) convertView.getTag();
        }
        String strDest = chattingListArrayList.get(position).getDestination2() ;
        holder.board.setText(chattingListArrayList.get(position).getBoardingLocation2());
        holder.dest.setText(strDest);
        holder.chatMember.setText("" + chattingListArrayList.get(position).getBoardingPeopleNum2());
        return convertView;
    }

    private class CustomViewHolder {
        TextView dest ;
        TextView board ;
        TextView chatMember ;
    }
}

listview 을 이용하는 경우에도 viewHolder 을 사용하여 데이터를 저장하는 것이 listview 을 상하 스크롤를 하더라도 데이터가 없어지는 현상을 방지할 수 있었다.

오늘도 즐~

반응형
Button crashButton = new Button(this);
crashButton.setText("Test Crash");
crashButton.setOnClickListener(new View.OnClickListener() {
   public void onClick(View view) {
       throw new RuntimeException("Test Crash"); // Force a crash
   }
});

addContentView(crashButton, new ViewGroup.LayoutParams(
       ViewGroup.LayoutParams.MATCH_PARENT,
       ViewGroup.LayoutParams.WRAP_CONTENT));

이건 뭐할 떄 쓰면 좋은가 하는 생각이 든다.  앱을 만들고 나서 누군가에게 전달을 했는데, 그 앱에서 에러가 난다고 한다. 흐미... 내가 테스트 할 때는 문제가 없었던 것 같은데 ...

그래서 이런 저런 방법을 찾아보던 중에 오호라... 이런 것도 있네... 각설하고... 일단 개발자 가이드를 읽어보자. 불행하게도 지금(2021.08.07일)까지는 한글로 된 문서가 안드로이드에 적합하게 되어 있지 않은 것 같다. 어쩔 수 없어 영문 사이트를 보면서 따라하기...(크롬의 자동번역기능을 이용해서)

https://firebase.google.com/docs/crashlytics/get-started?hl=en&platform=android 

 

Firebase Crashlytics 시작하기

iOS Android Unity 이 빠른 시작에서는 Firebase Crashlytics SDK를 사용해 앱에 Firebase Crashlytics를 설정하여 Firebase Console에서 포괄적인 비정상 종료 보고서를 확인하는 방법을 설명합니다. 시작하기 전에 아

firebase.google.com

https://firebase.google.com/docs/crashlytics/get-started?platform=Android 

 

Firebase Crashlytics 시작하기

iOS Android Unity 이 빠른 시작에서는 Firebase Crashlytics SDK를 사용해 앱에 Firebase Crashlytics를 설정하여 Firebase Console에서 포괄적인 비정상 종료 보고서를 확인하는 방법을 설명합니다. 시작하기 전에 아

firebase.google.com

내용은 같은 내용이지만, 아래 링크는 안드로이드에 대한 설며이 없고, 위에 링크는 설명은 있지만, 영문 페이지이고, 한글은 지원 하지 않는다. 아직 한글 사용자가 많지 않아서 인지... 흠흠흠...

뭐 하여간 크롬이 지원하는 자동번역기능을 이용해서 살펴보면...

위 그림 처럼 firebase console 에서 Crashlytics 을 들어가 보면 모래시계가 나오고 계속해서 기다린다고 되어 있다.  그래서 설명을 읽고 Crashlytics 을 활성화 하고  그 다음 할일은 gradle 을 수정하는 것이다...

먼저 project 의 gradle 파일에 아래 classpath 을 추가한다.

    dependencies {
        ...

        // Add the Crashlytics Gradle plugin
        classpath 'com.google.firebase:firebase-crashlytics-gradle:2.7.1'
    }
}

 다음은 apps 의 gradle 파일에 추가한다.

apply plugin: 'com.google.firebase.crashlytics'

...


dependencies {
    // Import the BoM for the Firebase platform
    implementation platform('com.google.firebase:firebase-bom:28.3.0')

    // Declare the dependencies for the Crashlytics and Analytics libraries
    // When using the BoM, you don't specify versions in Firebase library dependencies
    implementation 'com.google.firebase:firebase-crashlytics'
    implementation 'com.google.firebase:firebase-analytics'
}

 

그리고 나서 gradle 파일에서 sync 을 눌러서 필요한 파일들을 준비하고 나면 그다음은 마지막으로 MainActivity 에 아래 코드를 넣고 실행을 한번 하는 것이다. 

Button crashButton = new Button(this);
crashButton.setText("Test Crash");
crashButton.setOnClickListener(new View.OnClickListener() {
   public void onClick(View view) {
       throw new RuntimeException("Test Crash"); // Force a crash
   }
});

addContentView(crashButton, new ViewGroup.LayoutParams(
       ViewGroup.LayoutParams.MATCH_PARENT,
       ViewGroup.LayoutParams.WRAP_CONTENT));

이 코드는 고민할 것도 없다.  MainActivity 의 onCreate 에 넣고 실행하면 화면에 내가 design 하지 않은 버튼이 하나 나온다. Test Crash 라고 ... 그럼 그냥 한번 눌러주면 Firebase 의 console 화면이 변한다.. 아래 그림 처럼

 

그러면 이제 준비가 끝난 것이다.  그럼 이제 MainActivity 에 넣어던 위에 Test Crase 버튼은 필요가 없다. comment 처리를 하고 내가 만든 앱을 실행해 보는 것이다. 그러면 혹시나 모르는 오류가 발생했을 떄, console 에서 리스트를 확인해 볼 수 있다.  원격지에 있는 사람이 사용하다가 오류를 발생시키더라도 그의 폰에서 로그를 받아올 필요가 없어지는 것이다.

오호~ 이제 debug 는 끝났다. 

반응형

 

https://pixabay.com/ko/photos/%EA%BD%83-%EB%93%A4%EA%BD%83-%EA%B0%80%EC%9D%84-%EC%8B%9D%EB%AC%BC-%EC%95%BC%EC%83%9D%ED%99%94-3729845/

인터넷 펌.

코스모스가 피는 시기는 가을인가 ?

날이 더워 어서 왔으면 하는 바램은 나만인가 ?

시간이 가는 것도 그닥 기쁜(?)일은 아니기는 하지만.

코시대 이제 그만 물러나길 바래봄...

반응형

'그냥글쓰기' 카테고리의 다른 글

windows 바탕화면  (0) 2021.12.21
구글 애드센스 Google Adsense 에서 메일을 받았다.  (0) 2021.12.21
어부바 귀요미  (0) 2021.08.03
둘꽃  (0) 2021.07.24
망부석(?) 개인형 이동장치  (0) 2021.07.01

귀요미...
어부바...
가득 가득 채워주기 바래

반응형

'그냥글쓰기' 카테고리의 다른 글

구글 애드센스 Google Adsense 에서 메일을 받았다.  (0) 2021.12.21
들꽃  (0) 2021.08.04
둘꽃  (0) 2021.07.24
망부석(?) 개인형 이동장치  (0) 2021.07.01
들꽃2  (0) 2021.06.30

앞에 이야기를 보고 준비를 잘 했다면 이제 하나씩 만들어 보자.

카카오 지도 준비는 되었으니, 이제 카카오 개발자 페이지에서 주변 정보를 수집할 방법에 대하여 생각해 보자, 구글에서도 place API 을 지원하고 있으나, 사용에 부담이 되는 것은 아무래도 간혹 나오는 영문 데이터 떄문이다, 아직은 한국적인 느낌이 들지 않는다.

    <uses-permission android:name="android.permission.INTERNET" />

먼저 manifasts 파일에 인터넷 사용을 위한 권한등록을 한다. 데이터는 retrofit API을 이용해서 받아올 꺼니까

    implementation 'com.squareup.retrofit2:retrofit:2.7.2'
    implementation 'com.squareup.retrofit2:converter-gson:2.7.2'
    implementation 'com.squareup.retrofit2:converter-simplexml:2.1.0'

다음은 gradle 파일에 retrofit 사용을 위해서 implementation 을 선언한다. 나는 데이터를 json 방식으로 받아서 처리를 할 것이라서 simplexml 을 없어도 되나. 데이터를 혹시나 xml 구조로 받아야 하는 경우가 있을 때는 기술할 필요가 있다.

이제 카카오개발자 페이지에서 새로 앱을 만들기 등록을 하고, API 키를 받는다...(저 그림의 키는 일부이니 붙여넣기를 해도 소용이 없을 듯...)

그리고 플랫폼에 android 앱에 대한 정보를 하나 등록을 해야 하고, 키 해시는 앱을 실행해서 나오는 키 해시 값을 등록해 주어야 카카오 지도를 활용할 수 있다.

    public PackageInfo getPackageInfo(final Context context, int flag) {
        try {
            return context.getPackageManager().getPackageInfo(context.getPackageName(), flag);
        } catch (PackageManager.NameNotFoundException e) {
            Log.w(TAG, "Unable to get PackageInfo", e);
        }
        return null;
    }

    public String getKeyHash(Context context) {
        PackageInfo packageInfo = getPackageInfo(context, PackageManager.GET_SIGNATURES);
        if (packageInfo == null)
            return null;

        for (Signature signature : packageInfo.signatures) {
            try {
                MessageDigest md = MessageDigest.getInstance("SHA");
                md.update(signature.toByteArray());
                return Base64.encodeToString(md.digest(), Base64.NO_WRAP);
            } catch (NoSuchAlgorithmException e) {
                Log.e("getKeyHash", "Unable to get MessageDigest. signature=" + signature, e);
            }
        }
        return null;
    }

구글링을 해보면 다 나오겠지만, 위와 같이 코드를 넣어 실행 결과를 받아보면 해시값을 알 수 있다.

이제 주변 정보를 얻기 위해서. 카카오의 로컬 서비스를 호출할 준비를 해 보자.

https://developers.kakao.com/docs/latest/ko/local/dev-guide

 

Kakao Developers

카카오 API를 활용하여 다양한 어플리케이션을 개발해보세요. 카카오 로그인, 메시지 보내기, 친구 API, 인공지능 API 등을 제공합니다.

developers.kakao.com

아직 까지는 (2021.08.현재) rest 호출만 지원하고 있다 하니, 앞에서 준비할 것 처럼 http 통신을 해서 결과를 수집해야 할 것 같다.

내가 사용할 방법은 키워드를 통해서 주변을 찾아 보는 것이다. 

 

이런 내용이 있는 위치 부터 읽어 본다.  그리고 응답을 받아서 관리하기 위해서 아래와 같이 응답 구조체 class 을 만들었다.

package ...

import com.google.gson.annotations.SerializedName;

import java.util.ArrayList;

public class ResponseBean {
    @SerializedName("meta")
    Meta meta ;
    @SerializedName("documents")
    ArrayList<Documents> documents ;

    public Meta getMeta() {
        return meta;
    }

    public ArrayList<Documents> getDocuments() {
        return documents;
    }

    public void setMeta(Meta meta) {
        this.meta = meta;
    }

    public void setDocuments(ArrayList<Documents> documents) {
        this.documents = documents;
    }

    public class Meta {
        @SerializedName("same_name")
        SameName sameName ;
        @SerializedName("pageable_count")
        int pageableCount ;
        @SerializedName("total_count")
        int totalCount ;
        @SerializedName("is_end")
        boolean isEnd ;

        public SameName getSameName() {
            return sameName;
        }

        public int getPageableCount() {
            return pageableCount;
        }

        public int getTotalCount() {
            return totalCount;
        }

        public boolean isEnd() {
            return isEnd;
        }

        public void setSameName(SameName sameName) {
            this.sameName = sameName;
        }

        public void setPageableCount(int pageableCount) {
            this.pageableCount = pageableCount;
        }

        public void setTotalCount(int totalCount) {
            this.totalCount = totalCount;
        }

        public void setEnd(boolean end) {
            isEnd = end;
        }
    }

    public class Documents {
        @SerializedName("place_name")
        String placeName ;
        @SerializedName("distance")
        String distance ;
        @SerializedName("place_url")
        String placeUrl ;
        @SerializedName("category_name")
        String categoryName ;
        @SerializedName("address_name")
        String addressName ;
        @SerializedName("road_address_name")
        String roadAddressName ;
        @SerializedName("id")
        String id ;
        @SerializedName("phone")
        String phone;
        @SerializedName("category_group_code")
        String categoryGroupCode ;
        @SerializedName("category_group_name")
        String categoryGroupName ;
        @SerializedName("x")
        String posX ;
        @SerializedName("y")
        String posY ;

        public String getAddressName() {
            return addressName;
        }

        public String getCategoryGroupCode() {
            return categoryGroupCode;
        }

        public String getCategoryGroupName() {
            return categoryGroupName;
        }

        public String getCategoryName() {
            return categoryName;
        }

        public String getDistance() {
            return distance;
        }

        public String getId() {
            return id;
        }

        public String getPhone() {
            return phone;
        }

        public String getPlaceName() {
            return placeName;
        }

        public String getPlaceUrl() {
            return placeUrl;
        }

        public double getPosX() {
            return Double.parseDouble(posX);
        }

        public double getPosY() {
            return Double.parseDouble(posY);
        }

        public String getRoadAddressName() {
            return roadAddressName;
        }

        public void setAddressName(String addressName) {
            this.addressName = addressName;
        }

        public void setCategoryGroupCode(String categoryGroupCode) {
            this.categoryGroupCode = categoryGroupCode;
        }

        public void setCategoryGroupName(String categoryGroupName) {
            this.categoryGroupName = categoryGroupName;
        }

        public void setCategoryName(String categoryName) {
            this.categoryName = categoryName;
        }

        public void setDistance(String distance) {
            this.distance = distance;
        }

        public void setId(String id) {
            this.id = id;
        }

        public void setPhone(String phone) {
            this.phone = phone;
        }

        public void setPlaceName(String placeName) {
            this.placeName = placeName;
        }

        public void setPlaceUrl(String placeUrl) {
            this.placeUrl = placeUrl;
        }

        public void setPosX(String posX) {
            this.posX = posX;
        }

        public void setPosY(String posY) {
            this.posY = posY;
        }

        public void setRoadAddressName(String roadAddressName) {
            this.roadAddressName = roadAddressName;
        }
    }

    public class SameName {
        @SerializedName("region")
        String[] region ;
        @SerializedName("keyword")
        String keyWord ;
        @SerializedName("selected_region")
        String selectedRegion;

        public String getSelectedRegion() {
            return selectedRegion;
        }

        public String getKeyWord() {
            return keyWord;
        }

        public String[] getRegion() {
            return region;
        }

        public void setRegion(String[] region) {
            this.region = region;
        }

        public void setKeyWord(String keyWord) {
            this.keyWord = keyWord;
        }

        public void setSelectedRegion(String selectedRegion) {
            this.selectedRegion = selectedRegion;
        }
    }
}

부분 쪼개서 class 로 나누는 것도 좋기는 하나, 뭐 그러나 저러나 비슷한 것 같아서... 그냥 파일 하나에 다 담아서 정리를 했다. 그 다음은 이제 저 class 을 이용해서 호출하는 api 을 하나 만들어 보자 (2021.08.21 아래 소스 일부 수정)

package ...

import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Headers;
import retrofit2.http.Query;

/**
 * http 호출을 위한 API
 *     앱 등록 및 사용자 설정 필수 category
 *
 * query	            String	검색을 원하는 질의어	O
 * category_group_code	String	카테고리 그룹 코드 * 결과를 카테고리로 필터링을 원하는 경우 사용	X
 * x	                String	중심 좌표의 X값 혹은 longitude * 특정 지역을 중심으로 검색하려고 할 경우 radius와 함께 사용 가능	X
 * y	                String	중심 좌표의 Y값 혹은 latitude * 특정 지역을 중심으로 검색하려고 할 경우 radius와 함께 사용 가능	X
 * radius	            Integer	중심 좌표부터의 반경거리. 특정 지역을 중심으로 검색하려고 할 경우 중심좌표로 쓰일 x,y와 함께 사용 * 단위 meter, 0~20000 사이의 값	X
 * rect	                String	사각형 범위내에서 제한 검색을 위한 좌표. 지도 화면 내 검색시 등 제한 검색에서 사용 가능 * 좌측 X 좌표,좌측 Y 좌표, 우측 X 좌표, 우측 Y 좌표 형식	X
 * page	                Integer	결과 페이지 번호 * 1~45 사이의 값 (기본값: 1)	X
 * size	                Integer	한 페이지에 보여질 문서의 개수 * 1~15 사이의 값 (기본값: 15)	X
 * sort	                String	결과 정렬 순서, distance 정렬을 원할 때는 기준 좌표로 쓰일 x, y와 함께 사용 * distance 또는 accuracy (기본값: accuracy)	X
 *
 * category_group_code  String  카테고리 코드 O
 * x                    String             중심 좌표의 X값 혹은 longitude * 특정 지역을 중심으로 검색하려고 할 경우 radius와 함께 사용 가능.  (x,y,radius) 또는 rect 필수
 * y                    String             중심 좌표의 Y값 혹은 latitude * 특정 지역을 중심으로 검색하려고 할 경우 radius와 함께 사용 가능.  (x,y,radius) 또는 rect 필수
 * radius               Integer 중심 좌표부터의 반경거리. 특정 지역을 중심으로 검색하려고 할 경우 중심좌표로 쓰일 x,y와 함께 사용. 단위 meter, 0~20000 사이의 값  (x,y,radius) 또는 rect 필수
 * rect                 String  사각형 범위내에서 제한 검색을 위한 좌표 * 지도 화면 내 검색시 등 제한 검색에서 사용 가능 * 좌측 X 좌표, 좌측 Y 좌표, 우측 X 좌표, 우측 Y 좌표 형식 * x, y, radius 또는 rect 필수 X
 * page                 Integer 결과 페이지 번호 * 1~45 사이의 값 (기본값: 1)  X
 * size                 Integer 한 페이지에 보여질 문서의 개수 * 1~15 사이의 값 (기본값: 15) X
 * sort                 String  결과 정렬 순서, distance 정렬을 원할 때는 기준좌표로 쓰일 x, y 파라미터 필요 * distance 또는 accuracy (기본값: accuracy) X
 */
public interface RetrofitApi {
    @Headers("Authorization:KakaoAK 647a2bd........0b9d8")
    @GET("/v2/local/search/keyword.json")
    Call<ResponseBean> getKeywordData (@Query(value="query", encoded = true) String strAddr,
                                       @Query("x") double x,
                                       @Query("y") double y,
                                       @Query("radius") int radius,
                                       @Query("page") int page
                                );

    @Headers("Authorization:KakaoAK 647a2........970b9d8")
    @GET("/v2/local/search/category.json")
    @GET("/v2/local/search/category.json")
    Call<ResponseBean> getCategoryData (@Query("category_group_code") String categoryCode,
                                        @Query("x") double x,
                                        @Query("y") double y,
                                        @Query("radius") int radius,
                                        @Query("rect") String rect,
                                        @Query("page") int page);
    );
}

여기서 ResponseBean 은 위에서 말한 class 이름이고, @Headers 에 들어 있는 문장에 키값은 위에서 받은 rest api 키 이니 참고 하시길...  자 이제 호출해 볼까 ?  아래와 같이 화면의 버튼 event 등에서 getData을 호출하면 된다. 

넘어가는 파라미터는 검색할 때 사용할 strKeyword 그리고 중심이 되는 x, y 좌표(Longitude, Latitude)값, 그리고 반경(radius), 그리고 받아올 페이지의 시작값(iPage) 

페이지의 시작값은 난 무조건 1페이지 분량만 받아서 할 거라서 1로 설정해서 받아왔지만, 주변 검색을 더 하고 싶으면 페이지 번호를 계속 변경해서 받아오면 된다.  그 페이지가 마지막 인지 여부를 응답구조체 중에서 meta is_end 이 값을 보고 알 수 있다.

    public void getData(String strKeyword, double x, double y, int radius, int iPage) {
        Log.e(TAG, "(" + x + "," + y + ")" + strKeyword);
        service.getKeywordData(strKeyword, x, y, radius, iPage).enqueue(new Callback<ResponseBean>() {
            @Override
            public void onResponse(Call<ResponseBean> call, Response<ResponseBean> response) {
                Log.i(TAG, "code=" + response.code() + "" ) ;
                try {
                    for (ResponseBean.Documents documents : response.body().getDocuments()) {
                        Log.e(TAG, documents.getPlaceName());
                        MapPoint mapPoint = MapPoint.mapPointWithGeoCoord(documents.getPosY(), documents.getPosX());
                        addMarker(mapPoint, documents.getPlaceName());
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(Call<ResponseBean> call, Throwable t) {

                Log.e(TAG, t.toString()) ;
                t.printStackTrace();

            }
        });
    }

그리고 결과를 받아 왔다면 addMarker 함수를 호출해서 지도에 마커들을 출력하면 끝...

 

다른 것들은 이제 다음에...

반응형

오늘 부터 앱을 하나 만들꺼다... 생각만 하고 있던... 그래서 하나씩 도전을 해 볼껀데,  잘 할 수 있을까 ???

구현 목적 : 집에 가는 길에 약국에 들러서 약을 하나 사 가야겠다는 생각을 하고 퇴근을 했는데,  땀 삐질 거리며 집으로 와서 보니 으헉~ 집에 오는 길에 있는 약국앞을 그냥 지나쳐 온 것이다.   그래서 준비를 하기 시작 했다.

할려는 방안 : 카카오 개발자 페이지를 활용해서 앱에 지도를 넣고, 주변 검색 기능도 넣는다. 그리고 그걸 이용해서 집에 가는 길에 있는 약국 위치에 기록을 해 두는 것이지 , 여기 근처에 가면 해열제 하나를 꼭 사야 한다고...

생각은 되었으니, 이제 구현을 하나씩 해 보자.

준비물 : 카카오 개발자 페이지 등록, API 키 받기, 통신을 위한 retrofit 에 대한 이해. 그리고 개발툴 (android studio Arctic Fox 2020.3.1 버전 기준 : 2021.07.29쯤 업데이트가 적용 되었다)

먼저 카카오 지도 연동에 대한 이야기는 이제 내용을 참조한다.

https://billcorea.tistory.com/23

 

Kakao 지도 연동...

몇해전에는 카카오 지도를 연동하는 데, 애로 사항을 많이 느꼈다. 카카오의 기술지원은 어디에 있는 것인지 찾을 수 도 없고... 이번에 다시금 도전~ apis.map.kakao.com/android/ 이 페이지는 예나 지금

billcorea.tistory.com

다음 준비를 해야 할 부분은 아무래도 backgroud location 에 대한 준비를 해야 할 것 같다.

android 가 API29 이상으로 넘어가면서 background location 에 대한 권한에 제한이 많아지는 것 같다.

https://developer.android.com/training/location/permissions?hl=ko 

 

위치 정보 액세스 권한 요청  |  Android 개발자  |  Android Developers

사용자 개인 정보를 보호하려면 위치 서비스를 사용하는 앱에서 위치 정보 액세스 권한을 요청해야 합니다. 위치 정보 액세스 권한을 요청할 때는 다른 런타임 권한을 요청할 때와 동일한 권장

developer.android.com

 

꼭 읽어보고 가급적이면 github 에 올라와 있는 예제도 살펴볼 필요가 있을 것 같다.

https://github.com/android/location-samples/tree/main/Geofencing

 

GitHub - android/location-samples: Multiple samples showing the best practices in location APIs on Android.

Multiple samples showing the best practices in location APIs on Android. - GitHub - android/location-samples: Multiple samples showing the best practices in location APIs on Android.

github.com

 

여하튼 여기 까지가 내용들을 잘 살펴 보았다면, 준비는 끝일 것 같다.

이제 작성하는 건 다른 글에서...

반응형

android studio 에서 gradle build 을 하는 동안 서버 인증서 때문에 오류가 발생하는 경우를 경험하게 되었다. 

이런 경우 다음과 같은 해소 방안을 찾을 수 있을 것 같다.

Caused by: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

	at sun.security.validator.PKIXValidator.doBuild(Unknown Source)

	at sun.security.validator.PKIXValidator.engineValidate(Unknown Source)

	at sun.security.validator.Validator.validate(Unknown Source)

	at sun.security.ssl.X509TrustManagerImpl.validate(Unknown Source)

	at sun.security.ssl.X509TrustManagerImpl.checkTrusted(Unknown Source)

	at sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(Unknown Source)

	... 62 common frames omitted

Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

	at sun.security.provider.certpath.SunCertPathBuilder.build(Unknown Source)

	at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(Unknown Source)

	at java.security.cert.CertPathBuilder.build(Unknown Source)

	... 68 common frames omitted



출처: https://yunyun-onon.tistory.com/205 [XY 염색체의 진지한 시선 블로그]

 

이런 오류들을 만나게 되는 경우 

1. 시작 -> 명령 프롬프트(cmd)를 실행한다.



2. cd ${자바 설치 경로}\jre\bin

자바 jre를 설치한 경로 안에 bin폴더로 이동하기 위해 해당 명령어를 실행한다.

(bin폴더 안에 keytool이 있기 때문에 이동하여 실행하고자 한다.)



3. keytool -import -file "${인증서 경로}\인증서명.crt" -keystore "${자바 설치 경로}\jre\lib\security\cacerts" -storepass "changeit"



출처: https://yunyun-onon.tistory.com/205 [XY 염색체의 진지한 시선 블로그]

 

그런데 문제는 내컴퓨터에 서버 인증서 파일이 있는가 하는 것이다.

나의 경우는 회사에서 받은 파일을 이용하여 해소 하기는 했는데,  개인적인 경우라면...

음...

반응형

앱을 만들다 보니 로딩 화면에서 광고문구등을 넣어서 사용하고 싶은 요청이 있다. 이런건 어떻게 ? 그냥 쉬운 생각으로 빈 activity 을 만들어서 잠시 보여주고 그냥 닫아 주면 되지 않을까 ? 그래서 다소 빈약한 layout을 가지는 activity을 하나 만들었다.

먼저 layout 

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="18dp"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingRight="18dp"
    android:paddingBottom="@dimen/activity_vertical_margin"

    tools:context=".LoadingPage">

    <TextView
        android:id="@+id/textView"
        android:layout_width="180dp"
        android:layout_height="65dp"
        android:text="@string/loading"
        android:textSize="34sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.593"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.421" />

    <TextView
        android:id="@+id/textView8"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="36dp"
        android:layout_marginTop="420dp"
        android:gravity="center_horizontal|center_vertical"
        android:textSize="20sp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView9"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="36dp"
        android:layout_marginTop="460dp"
        android:gravity="center_horizontal|center_vertical"
        android:textSize="20sp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView10"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="36dp"
        android:layout_marginTop="500dp"
        android:gravity="center_horizontal|center_vertical"
        android:textSize="20sp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

 결과물은 이런 정도 메시지가 들어가는 layout

예시물에는 사용자의 요청에 따라 일부 문구가 들어가기는 하지만, 

이건 단순 예제 이니 참고만...

 

 

 

 

 

 

 

 

 

다음은 activity을 만들면...

public class LoadingPage extends Activity implements View.OnClickListener {

    private static final String TAG = "LoadingPage";
    ActivityLoadingPageBinding binding ;
    SharedPreferences sp ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        sp = getSharedPreferences(getPackageName(), MODE_PRIVATE);
        binding = ActivityLoadingPageBinding.inflate(getLayoutInflater());
        View view = binding.getRoot() ;

        setContentView(view);

    }

    @Override
    protected void onStart() {
        super.onStart();
        startLoading();
    }

    private void startLoading() {
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                finish();
            }
        }, 3000);
    }

}

이런 정도의 코드... acitivity 의 lifecycle 을 보고 어디쯤에서 종료를 하게 만들껀가 그것을 고민했고, 다음은 열리자 마자 닫아 버리면 안되니까... handler 을 이용해서 약간의 지연 (3000ms = 3sec) 을 두고 앱을 종료해 주는 것으로 끝.

그럼 이걸 어떻게 호출하지 하는 건 다음과 같이 MainActvity 에서 호출하는 방식으로 처리를 했다.

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        ...
        
        Intent intent = new Intent(this, LoadingPage.class);
        startActivity(intent);
        
        ...
    
    }

이렇게 MainActivity 을 시작하자 마자 LoadingPage 을 호출하는 것으로 그렇게 하면 사용자는 메인화면위에 올라온 loading 페이지를 잠시 보고 사라지는 것을 보게될 것이다. 

 

이것으로 끝,,,

 

 

 

반응형

+ Recent posts