android. GCM(Google Cloud Messaging) 적용하기

1. 구글 개발자 콘솔에서 프로젝트 등록하고 GCM 항목 활성화

https://console.developers.google.com

2. 클라이언트 매니페스트(AndroidManifest.xml) 설정

...

<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.GET_ACCOUNTS" />

<permission 
 android:name="#the package name of your application#.permission.C2D_MESSAGE" 
 android:protectionLevel="signature" />
<uses-permission android:name="#the package name of your application#.permission.C2D_MESSAGE" />

...

<receiver
 android:name=".GcmBroadcastReceiver"
 android:permission="com.google.android.c2dm.permission.SEND" >
 <intent-filter>
  <action android:name="com.google.android.c2dm.intent.RECEIVE" />
  <category android:name="#the package name of your application#" />
 </intent-filter>
 <intent-filter >
  <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
  <category android:name="#the package name of your application#" />
 </intent-filter>
</receiver>
<service android:name=".GcmIntentService" />


3. 클라이언트 프로그램 코드 - 엑티비티 라이프 사이클에서 기기등록 처리

final String GCM_SENDER_ID = "000000000000";
final String PREF_NAME = "gcm_client_info";
final String PREF_KEY_REG_ID = "reg_id";
final String PREF_KEY_REG_APP_VER = "reg_app_ver";

final Function< Context, Integer > funcGetAppVersion = new Function< Context, Integer >(){
 @Nullable
 @Override
 public Integer apply( @Nullable Context context ){
  try {
   PackageInfo packageInfo = context.getPackageManager().getPackageInfo( context.getPackageName(), 0 );
   return packageInfo.versionCode;
  }catch( PackageManager.NameNotFoundException ex ){
   throw new RuntimeException( ex );
  }
 }
};

if( ConnectionResult.SUCCESS == GooglePlayServicesUtil.isGooglePlayServicesAvailable( this ) ){

 final SharedPreferences prefs = getSharedPreferences( PREF_NAME, Context.MODE_PRIVATE );
 String regId = prefs.getString( PREF_KEY_REG_ID, "" );
 int regAppVer = prefs.getInt( PREF_KEY_REG_APP_VER, Integer.MIN_VALUE );
 int curAppVer = funcGetAppVersion.apply( context );
 if( regAppVer != curAppVer || Strings.isNullOrEmpty( regId ) ){

  new Thread(){
   @Override
   public void run(){

    try{

     GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance( context );
     String newRegId = gcm.register( GCM_SENDER_ID );

     SharedPreferences.Editor editor = prefs.edit();
     editor.putString( PREF_KEY_REG_ID, newRegId );
     editor.putInt( PREF_KEY_REG_APP_VER, funcGetAppVersion.apply( context ) );
     editor.commit();

     //TODO
     // You must send client registration id to server
     // 발급받은 클라이언트 등록키를 서버로 보내는 코드를 추가해야 합니다

    }catch( IOException ex ){
     throw new RuntimeException( ex );
    }//try

   }
  }.start();

 }//if

}//if


4. 클라이언트 프로그램 코드 - 브로드캐스트 리시버에서 서버 메시지 수신

public class GcmBroadcastReceiver extends WakefulBroadcastReceiver{
 @Override
 public void onReceive( Context context, Intent intent ){
  ComponentName c = new ComponentName( context.getPackageName(), GcmIntentService.class.getName() );
  startWakefulService( context, ( intent.setComponent( c ) ) );
  setResultCode( Activity.RESULT_OK );
 }
}

5. 클라이언트 프로그램 코드 - 서비스에서 수신된 메세지에 대한 UI 처리(노티바에 표시)

public class GcmIntentService extends IntentService{

 public GcmIntentService(){
  super( "GcmIntentService" );
 }

 @Override
 protected void onHandleIntent( Intent intent ){

  int notifyId = 2626;//or else

  Bundle extras = intent.getExtras();

  if ( !extras.isEmpty() && GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals( GoogleCloudMessaging.getInstance( this ).getMessageType( intent ) ) ){

   NotificationManager nm = ( NotificationManager ) getSystemService( Context.NOTIFICATION_SERVICE );
   Intent it = new Intent( this, MainActivity.class );
   PendingIntent pi = PendingIntent.getActivity( this, 0, it, PendingIntent.FLAG_UPDATE_CURRENT );

   NotificationCompat.Builder builder =
     new NotificationCompat.Builder( this )
       .setSmallIcon( R.drawable.some_icon )
       .setContentTitle( extras.getString( "title" ) )
       .setContentText( extras.getString( "message" ) )
       .setContentIntent( pi );
   Notification notification = builder.build();
   nm.notify( notifyId, notification );
  }
  GcmBroadcastReceiver.completeWakefulIntent( intent );
 }

}

6. GCM 전송 테스트

아래 방법을 이용하면 별개의 서버 구성을 하지 않고도 GCM 전송과 클라이언트 동작을 테스트할 수 있습니다.

http://stackoverflow.com/questions/22168819/android-test-push-notification-google-cloud-messaging-online


다른글 읽기