2016년 4월 10일 일요일

GCM 2. AndroidManifest.xml 설정 및 화면구성

1. AndroidManifest.xml 설정 - 인터넷 사용권한과 리시버, 서비스들을 등록

GCM Permission : 디바이스에 GCM 서비스를 사용하기 위한 권한 설정
GCM Receiver : GCM을 받았을 때 동작하기 위한 리시버
GCM Listener Service : GCM을 요청을 대기하고 있는 리스너 서비스
InstanceID Listener Service : InstanceID 요청을 대기하고 있는 리스너 서비스
GCM Registration Service : GCM을 등록하기 위한 서비스

1) GCM Permission
아래 두개는 필수이고 다른 기능을 위해 두개를 더 추가해 준다.

android.permission.WAKE_LOCK
com.google.android.c2dm.permission.RECEIVE

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />

2) GCM Receiver
GCM을 받기 위한 리시버를 만들어야하는데 GCM 리시버는 특별히 구현할 필요가 없다.
GCM 라이브러리 안에 이미 구현체가 있기 때문에 정의만하면 된다.

<receiver
    android:name="com.google.android.gms.gcm.GcmReceiver"
    android:exported="true"
    android:permission="com.google.android.c2dm.permission.SEND">
    <intent-filter>
 <action android:name="com.google.android.c2dm.intent.RECEIVE" />
 <category android:name="com.example.rock.opregcheck" />
    </intent-filter>
</receiver>

3) GCM Listener Service
GCM 리스너 서비스는 GCM 메세지가 디바이스로 전송이되면 메세지를 받아서 처리하는 프로그램을
서비스로 정의한다. GCM 받아서 실제 Notification Center에 어떻게 나타내는지를 정의한다.
이후에 살펴볼 GcmListenerService.java에 내용을 구현할 것이다.

<service
    android:name=".GcmListenerService"
    android:exported="false">
    <intent-filter>
 <action android:name="com.google.android.c2dm.intent.RECEIVE" />
    </intent-filter>
</service>

4) InstanceID Listener Service
최근 GCM 서비스에서는 Instance ID를 사용한다. 이것은 Android, iOS의 고유한 ID로
GCM에서 디바이스를 구분하기 위한 것이다.
Instance ID를 위한 리스너를 GcmInstanceIDListenerService.java에서 구현할 것이다.

<service
    android:name=".GcmInstanceIDListenerService"
    android:exported="false">
    <intent-filter>
 <action android:name="com.google.android.gms.iid.InstanceID" />
    </intent-filter>
</service>

5) GCM Registration Service
실제 디바이스에서 Instance ID를 사용하여 디바이스를 GCM에 등록하고
디바이스 고유 토큰을 생성하기 위한 서비스를 GcmRegistrationIntentService.java에서 구현할 것이다.
<service
    android:name=".GcmRegistrationIntentService"
    android:exported="false">
</service>

* GCM을 사용하기 위한 메니페스트 파일 전체 내용은 다음과 같다.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.rock.opregcheck">
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <receiver
            android:name="com.google.android.gms.gcm.GcmReceiver"
            android:exported="true"
            android:permission="com.google.android.c2dm.permission.SEND">
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <category android:name="com.example.rock.opregcheck" />
            </intent-filter>
        </receiver>
        <service
            android:name=".GcmListenerService"
            android:exported="false">
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            </intent-filter>
        </service>
        <service
            android:name=".GcmInstanceIDListenerService"
            android:exported="false">
            <intent-filter>
                <action android:name="com.google.android.gms.iid.InstanceID" />
            </intent-filter>
        </service>
        <service
            android:name=".GcmRegistrationIntentService"
            android:exported="false">
        </service>
    </application>
</manifest>


2. 화면구성

1)activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.rock.opregcheck.MainActivity">
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="인스턴스ID 토큰 가져오기"
        android:id="@+id/btnGetInstanceId"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="33dp" />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/mInstanceIdTextView"
        android:layout_below="@+id/btnGetInstanceId"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="73dp" />
    <ProgressBar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/progressBar"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/mReceiveMessageTextView"
        android:layout_below="@+id/progressBar"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="53dp" />
</RelativeLayout>

2) demens.xml 저장
<resources>
    <!-- Default screen margins, per the Android Design guidelines. -->
    <dimen name="activity_horizontal_margin">16dp</dimen>
    <dimen name="activity_vertical_margin">16dp</dimen>
</resources>


3. QuickstartPreferences.java 추가

이 파일은 GCM Demo에서 사용하는 LocalBoardcast의 액션을 정의한 파일이다.

package com.example.rock.opregcheck;

/**
 * Created by rock on 2016-04-05.
 */
public class QuickstartPreferences {
    public static final String REGISTRATION_READY = "registrationReady";
    public static final String REGISTRATION_GENERATING = "registrationGenerating";
    public static final String REGISTRATION_COMPLETE = "registrationComplete";
    public static final String INSTANCE_ID_TOKEN = "instanceIdToken";
    public static final String GCM_MESSAGE_RECEIVE = "gcmMessageReceive";
    public static final String GCM_MESSAGE = "gcmMessage";
}


4. res/drawable 에 아이콘 추가

메세지 도착시 GcmListener 에서 알림을 띄울때 사용할 아이콘 (sendNotification)

댓글 없음 :

댓글 쓰기