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

gcmintentservice 문의드려요.

0 추천
public class GCMIntentService extends GCMBaseIntentService implements NetDefine {

	private static final String TAG = "GCM Tutorial::Service";

	public static NextListener listener;

	public GCMIntentService() {
		super(SENDER_ID);
	}

	Setting mSetting;

	@Override
	protected void onMessage(Context context, Intent intent) {
		mSetting = new Setting(context);

		if (intent.getAction().equals("com.google.android.c2dm.intent.RECEIVE")) {

			String option_push = mSetting.getString(KEY_PUSHCH, "");

			if ("true".equals(option_push)) {
				showMessage(context, intent);
			}
		}
	}

	@Override
	protected void onError(Context context, String msg) {
		// TODO Auto-generated method stub
		Log.w(TAG, "onError!! " + msg);
	}

	@Override
	protected void onRegistered(Context context, String regID) {
		// TODO Auto-generated method stub
		if (listener != null) {
			listener.next();
			listener = null;
		}
		mSetting = new Setting(context);
		mSetting.input(KEY_GCM_REGI_ID, regID);
	}

	@Override
	protected void onUnregistered(Context context, String regID) {
		// TODO Auto-generated method stub
		Log.w(TAG, "onUnregistered!! " + regID);
	}

	public void showToast() {
		Toast.makeText(this, "RegID 등록 완료", Toast.LENGTH_SHORT).show();
	}

	private void showMessage(Context context, Intent intent) {
		String title = intent.getStringExtra(KEY_TITLE);
		String msg = intent.getStringExtra(KEY_MSG);
		String section = intent.getStringExtra(KEY_SECTION);
		String category = intent.getStringExtra(KEY_CATEGORY);
		String no = intent.getStringExtra(KEY_NO);
		String fairurl = intent.getStringExtra(KEY_FAIRURL);

		NotificationManager notificationManager = (NotificationManager) context
				.getSystemService(Activity.NOTIFICATION_SERVICE);

		long time = new Date().getTime();
		String tmpStr = String.valueOf(time);
		String last4Str = tmpStr.substring(tmpStr.length() - 5);
		int notificationId = Integer.valueOf(last4Str);
		
		// // 해당 어플을 실행하는 이벤트를 하고싶을 때 아래 주석을 풀어주세요
		PendingIntent pendingIntent;

		String url = "section=" + section + "&category=" + category + "&no="
				+ no;

		int titleBar = DataManager.getCodeModel(Integer.parseInt(category)).getTitleText();
		
		pendingIntent = PendingIntent.getActivity(
				context,
				notificationId,
				new Intent(context, DetailNewsViewActivity.class)
						.putExtra(KEY_SUB_URL, url)
						.putExtra(KEY_SECTION, section)
						.putExtra(KEY_CATEGORY, category)
						.putExtra(KEY_TITLEBAR, titleBar)
						.putExtra(KEY_SUBJECT, msg)
						.putExtra(KEY_CHANNEL, true)
						.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK),
				PendingIntent.FLAG_UPDATE_CURRENT);

		Notification notification = new Notification();
		notification.icon = R.drawable.icon;
		notification.when = System.currentTimeMillis();

		notification.flags = Notification.FLAG_AUTO_CANCEL;
		notification.setLatestEventInfo(context, title, msg, pendingIntent);



		notificationManager.notify(notificationId, notification);

		Intent i = new Intent(context, GCMMessageView.class);
		i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
				| Intent.FLAG_ACTIVITY_CLEAR_TOP);
		i.putExtra(KEY_TITLEBAR, titleBar);
		i.putExtra(KEY_MSG, msg);
		i.putExtra(KEY_SUB_URL, url);
		i.putExtra(KEY_SECTION, section);
		i.putExtra(KEY_CATEGORY, category);
		i.putExtra(KEY_FAIRURL, fairurl);

		context.startActivity(i);

		{
			Vibrator vibe = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);

			// Wake Android Device when notification received
			PowerManager pm = (PowerManager) context
					.getSystemService(Context.POWER_SERVICE);
			final PowerManager.WakeLock mWakelock = pm.newWakeLock(
					PowerManager.FULL_WAKE_LOCK
							| PowerManager.ACQUIRE_CAUSES_WAKEUP, "GCM_PUSH");
			mWakelock.acquire();

			Timer timer = new Timer();
			TimerTask task = new TimerTask() {
				public void run() {
					mWakelock.release();
				}
			};
			vibe.vibrate(1000);
			timer.schedule(task, 5000);

		}

	}

	public interface NextListener {
		public void next();
	}

안녕하세요 GCM으로 푸시 사용중인데 앱을 처음 시작할 때  에러 종료가 되면서

int titleBar = DataManager.getCodeModel(Integer.parseInt(category)).getTitleText(); 이 부분에서 Integer.parseInt(category) 넘버포맷에러가 나네요. 

근데 showMessage 이 함수는 푸시가 발송 됐을 때만 타는 거 아닌가요?

메인액티비티에서 regist 함수 빼곤 호출하는 게 없는데 계속 넘버포맷 nullException 뜨면서 종료가 되는데

디버깅 했을 때 showMessage를 타는 것도 아니구요..;

그래서 형변환 하는 부분을 기본값으로 0을 줬더니 아예 푸시 보낼 때 다이얼로그가 실행될 때 뜨네요. 

이런 현상 겪으신 분들 있으신가요?

양꼬양 (2,040 포인트) 님이 2015년 6월 11일 질문
이런 현상이 아니라, 코드 구현이 잘못된것같은데요

답변 달기

· 글에 소스 코드 보기 좋게 넣는 법
· 질문에 대해 추가적인 질문이나 의견이 있으면 답변이 아니라 댓글로 달아주시기 바랍니다.
표시할 이름 (옵션):
개인정보: 당신의 이메일은 이 알림을 보내는데만 사용됩니다.
스팸 차단 검사:
스팸 검사를 다시 받지 않으려면 로그인하거나 혹은 가입 하세요.
...