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

GCM 질문입니다 onRegistered 호출이 되질않아 키 등록이 안되네요

0 추천

GCM 키등록이 안되서 질문올립니다. onRegistered 이 함수가 호출이 되질 않는것 같구요

이유는 다음 코드(registerGCM 함수) 를 지닌 클래스가 GCMBaseIntentService 를 상속받은 GCMIntentService 클래스와 같은 패키지가 아닐 경우에만 키등록이 안되더라구요. 꼭 반드시 같은 패키지 내에 있어야하는 이유가 뭔가요??

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		registerGCM();
	}
	
	private void registerGCM(){
		StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
		.detectDiskReads()             
		     .detectDiskWrites()
		.detectNetwork()              
		     .penaltyLog().build());

		GCMRegistrar.checkDevice(this);
		GCMRegistrar.checkManifest(this);
		final String regId = GCMRegistrar.getRegistrationId(this);
		GCMManager.regId = String.valueOf(regId);

		if (regId.equals("")) {
			GCMRegistrar.register(this, "99225203282");
		} else {
			Log.e("reg_id", regId);
		}
	}
}

다음은 같은 패키지에 있을 경우입니다.

 

다음은 GCMIntentService  에서 키 등록시 호출 되는 함수 입니다.

 

다음은 manifest입니다.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.testgcm"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="9"
        android:targetSdkVersion="17" />
	<!-- GCM Service -->
	<permission
        android:name="com.example.testgcm.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />
    <uses-permission android:name="com.example.testgcm.permission.C2D_MESSAGE" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.testgcm.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        
        <!-- GCM -->
		<receiver android:name="com.google.android.gcm.GCMBroadcastReceiver" 
		    android:permission="com.google.android.c2dm.permission.SEND">
		    <intent-filter>
		        <action android:name="com.google.android.c2dm.intent.RECEIVE"/>
		        <action android:name="com.google.android.c2dm.intent.REGISTRATION"/>
		    </intent-filter>
		</receiver>
		<service android:name="com.example.testgcm.GCMIntentService"></service>
    </application>
</manifest>

현재 위 manifest 파일에서 볼 수 있듯이 mainActivity 와 GCMIntentService 클래스가 같은 패키지에 있는 경우입니다.

물론 이 경우에는 키 등록도 되고 잘 작동하지만 GCMIntentService 패키지를 다른걸로 변경하고 <service android:name = "????"></service> 를 변경된 패키지이름으로 바꿔주면 키등록이 안되네요. 어떻게 해결할 수 있죠?

퍼미션 주는 부분도 변경된 패키지 이름으로 바꿔도 봤는데 그 경우에는 아에 그냥 빨간 에러가 발생하더라구요

껌돌이 (410 포인트) 님이 2013년 4월 30일 질문

1개의 답변

+1 추천
 
채택된 답변

인터넷사용 퍼미션과 계정정보 가져올수있는 퍼미션이 빠진것같네요.

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

를 추가해줘보세요.

 

필터에 카테고리도 빠진것같네요.

<intent-filter>
	<action android:name="com.google.android.c2dm.intent.RECEIVE"/>
	<action android:name="com.google.android.c2dm.intent.REGISTRATION"/>
	<category android:name="[패키지명]" />
</intent-filter>

도 추가해주세요.

얼룩돼지 (15,720 포인트) 님이 2013년 4월 30일 답변
껌돌이님이 2013년 4월 30일 채택됨
감사합니다. 해결되었습니다~
...