하루죙일 삽질했다….ㅠ.ㅠ
-우선은 Google API Console계정과 프로젝트를 생성한다.
1.https://code.google.com/apis/console/ 에 접속해서 계정을 만든다
2.Service 에서 Google Cloud Messaging for Android 를 ON
3.API access 에서 [Create New Server Key...]를 클릭해서 서버키를 만든다
4.프로젝트ID를 메모해둔다
2.Service 에서 Google Cloud Messaging for Android 를 ON
3.API access 에서 [Create New Server Key...]를 클릭해서 서버키를 만든다
4.프로젝트ID를 메모해둔다
https://code.google.com/apis/console/#project:[요부분이 ID]:access
-GCM라이브러리를 인스톨한다
sdk메니저를 킨후 extras에있는 Google Cloud Messaging for Android Library을 설치한다.
sdk메니저를 킨후 extras에있는 Google Cloud Messaging for Android Library을 설치한다.
-AndroidManifest.xml 파일을 수정한다.
1.퍼밋션 추가
<permission android:name=”패키지명.permission.C2D_MESSAGE” android:protectionLevel=”signature” />
<uses-permission android:name=”패키지명.permission.C2D_MESSAGE” />
<uses-permission android:name=”com.google.android.c2dm.permission.RECEIVE” />
<uses-permission android:name=”android.permission.INTERNET” />
<uses-permission android:name=”android.permission.GET_ACCOUNTS” />
<uses-permission android:name=”android.permission.WAKE_LOCK” />
<permission android:name=”패키지명.permission.C2D_MESSAGE” android:protectionLevel=”signature” />
<uses-permission android:name=”패키지명.permission.C2D_MESSAGE” />
<uses-permission android:name=”com.google.android.c2dm.permission.RECEIVE” />
<uses-permission android:name=”android.permission.INTERNET” />
<uses-permission android:name=”android.permission.GET_ACCOUNTS” />
<uses-permission android:name=”android.permission.WAKE_LOCK” />
2. 레시버 추가
<receiver android:name=”com.google.android.gcm.GCMBroadcastReceiver” android:permission=”com.google.android.c2dm.permission.SEND” >
<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" /> <category android:name="패키지명" /> </intent-filter>
</receiver>
<service android:name=".GCMIntentService" />
-라이브러리 설정 1.jar파일 복사
sdk폴더에 extras/google폴더에 들어가보면 gcm/gcm-client/dist/gcm.jar 파일이 있다. 이 파일을 프로젝트 libs폴더에 복사한다 그런다음 Build Path>Add External Achieves... 로 라이브러리추가한다
-메인 Activity수정
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
registerGcm();
}
public void registerGcm() {
GCMRegistrar.checkDevice(this); // 라이브러리설정이 제대로 않되면 여기서 익셉션이 발생한다
GCMRegistrar.checkManifest(this);
final String regId = GCMRegistrar.getRegistrationId(this);
if (regId.equals("")) {
GCMRegistrar.register(this, "프로젝트 아이디");
} else {
Log.e("reg_id", regId);
}
}
-레시버 추가
public class GCMIntentService extends GCMBaseIntentService {
private static void generateNotification(Context context, String message) {
int icon = R.drawable.ic_action_search;
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);
String title = context.getString(R.string.app_name);
Intent notificationIntent = new Intent(context, MainActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent = PendingIntent.getActivity(context, 0,
notificationIntent, 0);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, notification);
}
@Override
protected void onError(Context arg0, String arg1) {
}
@Override
protected void onMessage(Context context, Intent intent) {
// 메세지를 받으면 이곳에서 처리
String msg = intent.getStringExtra("message");
Log.e("getmessage", "getmessage:" + msg);
generateNotification(context, "push");
}
@Override
protected void onRegistered(Context context, String reg_id) {
Log.e("키등록", reg_id);
}
@Override
protected void onUnregistered(Context arg0, String arg1) {
Log.e("키제거", "---");
}
}
-실행해서ID취득하기 실행하면 아래와같은 로그가 출력된다
reg_idAPA91bG8xqTYD6MLH9AfV8OPLfIbTZH9fCk8LxEY1eV0JCwCyCRO0_rE07ecKHOhFKc3GeSvbpuF9OOpbbdXuNN5COhNOQO3WZNE9HQeDM3hsL-Bq4I2oktb5x17rNPo5SowlwIfh9k2SFLrlzjERgZ2AxzEG-nDrg
이걸로 ID취득 완료
-동작확인
아래의 페이지에서 PUSH를 보낼수 있다
구글의 API Key와 로그에서 출력된 ID 멧세지를 입력하고 send버튼을 클릭하면 OK!!!!
참고로 위 페이지의 멧세지 키는 [message]이다
참고로 위 페이지의 멧세지 키는 [message]이다
댓글 없음:
댓글 쓰기